Clear Filters
Clear Filters

event.listener not working when called within class instance

2 views (last 30 days)
Dear Forum,
I encounter some problems when using event.listener class. For testing I've implemented the following code:
Event-triggering class:
classdef Subject < handle
events
event
end
properties
end
end
Responding class:
classdef Object < handle
properties
listen;
end
methods
function setListener(obj, subject, event)
%obj.listen = event.listener(subject, event, @obj.respond);
obj.listen = addlistener(subject, event, @obj.respond);
end
end
methods
function respond(varargin)
disp('Event has occured');
end
end
end
And a tiny test script:
% create instances
subject = Subject();
object = Object();
% set listener
object.setListener(subject, 'event')
% notify object
notify(subject, 'event');
When switching the statements
%obj.listen = event.listener(subject, event, @obj.respond);
obj.listen = addlistener(subject, event, @obj.respond);
in class Object, I will get the following error:
Struct contents reference from a non-struct array object.
Error in Object/setListener (line 12)
obj.listen = event.listener(subject, event, @obj.respond);
Error in testListener (line 6)
object.setListener(subject, 'event')
To my understanding this should work.
Moreover, I can establish the connection, when creating the listener from my script:
l = event.listener(subject, 'event', @object.respond)
Is it not possible to call event.listener from within an object?
I am using Matlab 2016a.
Can someone please help?
Thanks,
Daniel

Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!