Implementing listener and callback, infinite loop

5 次查看(过去 30 天)
Hi,
My problem follows the one I described here:
So in my example, a class "Car" as properties "Brand" and "Color".
Brand can only be 'Ferrari' or 'Mercedes', and 'Color' can only be Yellow or Red for the Ferrari and Black or Silver for the Mercedes.
I did put Color as Dependent and added a set method with a sort of pair property of color.
When set or get is called for Color, its a "twin" property that holds the value.
I have a list of valid Color input for the Ferrari and the Mercedes, so that when setting the Color of the car it as to be right.
It works fine, but if I change the Brand, then the Color is invalid but there is no check for that.
So I would like the Color Prop to change to the first Valid one (in the list) whenever "Brand" is modified.
For that I tried to implement a property that listen to the Brand property. ==>"BrandChangeListener"
I made the Car class to inherit form handle. Then I was getting a warning with my set methods, so I modified them from: obj = set.propName(obj, value) to set.propName(obj, value), so that it does not return the object anymore. That fixed the warning.
In the constructor I tried to implement a listener:
myCar.BrandChangeListener = event.proplistener(myCar, findprop(myCar, 'brand'),'PostSet', @ResetColor);
On this line I get the error: While adding a PostSet listener, property 'brand' in class 'car' is not defined to be SetObservable.
  1 个评论
Stephen23
Stephen23 2017-12-4
@Laurent Davenne: this is not twitter. I removed the useless # characters from your tags.

请先登录,再进行评论。

采纳的回答

Adam
Adam 2017-12-4
编辑:Adam 2017-12-4
Personally I always use addlistener syntax when adding a listener. I am not familiar with your syntax, but it seems it will still work. The 'See Listen for Changes to Property Values.' link takes you to a page where all the examples use addlistener, but since you are getting this error message I assume the two syntaxes are interchangeable.
Basically you need to just follow what the error tells you and add the 'SetObservable' attribute to your 'brand' property
e.g.
properties( SetObservable, Access = public )
brand
end
  4 个评论
Laurent Davenne
Laurent Davenne 2017-12-4
编辑:Laurent Davenne 2017-12-4
Actually I do not even know why I need this 3rd input to make it work? Just saw this in a code example.
The doc says you need at least 2 inputs, and if more their use is explicit.
Why do I need a 3rd "empty" one?
Adam
Adam 2017-12-4
Default inputs for a callback are always the source and the event data. For a regular callback these will be the first two arguments. For a callback that is part of an object 'obj' will always be the first argument and source and event data will be the 2nd and 3rd arguments unless you call your callback as:
@(src,evt) obj.ResetColor( )
Again your syntax is one I am not familiar with though as you do not appear to need to attach obj to your call to ResetColor so I can only assume this is embedded into the event.proplistener creation.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by