Create an empty array of class event.listener
5 次查看(过去 30 天)
显示 更早的评论
Is it possible to create an empty array of the class event.listener?
I want to create event listeners (which calls callbackFcn when event listenEvent occurs for object listenObj) and save the handles to the event listeners in the property hListenerArray of an object obj so that the delete method of obj can delete the event listeners. My idea was to do this:
obj.hListenerArray(end+1) = addlistener(listenObj, listenEvent, callbackFcn);
but it fails
??? The following error occurred converting from event.listener to double: Error using ==> double Conversion to double from event.listener is not possible.
I think this is because the property hListenerArray is initialized as class double.
0 个评论
采纳的回答
Richard
2011-3-17
You can create empty objects with the static empty() method of a class, as documented at http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html. In this case using "event.listener.empty" as the initial value for the hListener property should work:
properties
hListenerArray = event.listener.empty;
end
An additional comment: if you use the event.listener constructor to create the listeners instead of using addlistener then you will not have to write a delete method because you will have the only reference to them in hListenerArray and they will simply go out of scope and delete themselves when obj is deleted. addlistener is adding an extra reference to the object being listened to and you do not want that in this case.
obj.hListenerArray(end+1) = event.listener(listenObj, listenEvent, callbackFcn);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!