Implementing listener and callback, infinite loop

조회 수: 3 (최근 30일)
Laurent Davenne
Laurent Davenne 2017년 12월 4일
댓글: Adam 2017년 12월 4일
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에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by