Create an empty array of class event.listener

5 visualizaciones (últimos 30 días)
Daniel Shub
Daniel Shub el 17 de Mzo. de 2011
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.

Respuesta aceptada

Richard
Richard el 17 de Mzo. de 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);

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by