フィルターのクリア

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 Center および File ExchangeConstruct 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