Effacer les filtres
Effacer les filtres

Implementing listener and callback, infinite loop

5 vues (au cours des 30 derniers jours)
Laurent Davenne
Laurent Davenne le 4 Déc 2017
Commenté : Adam le 4 Déc 2017
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 commentaire
Stephen23
Stephen23 le 4 Déc 2017
@Laurent Davenne: this is not twitter. I removed the useless # characters from your tags.

Connectez-vous pour commenter.

Réponse acceptée

Adam
Adam le 4 Déc 2017
Modifié(e) : Adam le 4 Déc 2017
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 commentaires
Laurent Davenne
Laurent Davenne le 4 Déc 2017
Modifié(e) : Laurent Davenne le 4 Déc 2017
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 le 4 Déc 2017
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by