Create an empty array of class event.listener

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Richard
Richard il 17 Mar 2011
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);

Più risposte (0)

Categorie

Scopri di più su Software Development Tools in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by