Listener callback casts custom event data back to event.eventData?

12 次查看(过去 30 天)
I have a simple subclass of event.EventData to pass data to listeners:
classdef (ConstructOnLoad) tuningEventData < event.EventData
properties
path
value
end
methods
function obj = tuningEventData(path, value)
obj.path = path;
obj.value = value;
end
end
end
I trigger an event, passing a tuningEventData object:
notify(obj, 'tuningChange', tuningEventData(path, val));
Now I have one listener receiving this and triggering yet another event in its callback method, which I'd like to pass on the same tuningEventData object:
function handleTuningChange(obj, src, data)
notify(obj, 'tuningChange', data);
end
But listeners to this new event do not receive the tuningEventData object. They receive an EventData object instead which obviously doesn't have the path and value properties.
The only way to pass on a tuningEventData object seems to be creating a new object in the first listener's callback:
function handleTuningChange(obj, src, data)
notify(obj, 'tuningChange', tuningEventData(data.path, data.value));
end
Why can't I just send the data object straight on? Is my tuningEventData class missing something?

回答(1 个)

Taylan Kanber
Taylan Kanber 2022-1-10
Change obj.path and obj.value to data.path and data.value

类别

Help CenterFile Exchange 中查找有关 Events 的更多信息

产品


版本

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by