Create an empty array of class event.listener

조회 수: 4 (최근 30일)
Daniel Shub
Daniel Shub 2011년 3월 17일
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.

채택된 답변

Richard
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개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by