How To Avoid Triggering Property Setter Method
Show older comments
We are using set methods in a number of our handle derived classes to do extended property validations.
The general requirement of what we did before was:
- Check that the new value is OK
- Backup the old value
- Set the new value
- Run a series of calculations
- If an error occurrs during calculations, restore the old value
Previously we were using dynamic properties for this which allowed for a completely custom set function to be used. We are however now trying to switch to non-dynamic properties due to the performance penalties of dynamic prop lookups.
In order to acheive similar behaviour, we need to use the set.PropName function for each of the non-dynamic properties. Ideally this set function would just call our original set method directory:
function set.PropName(obj, val)
obj.customSetter('PropName',val);
end
However this according to the help text won't work because if we set the property value in customSetter, then "property assignments made from functions called by a set method do call the set method" which would surely cause an infinite loop, and seems a rather strange design "feature".
Instead we end up with this kind of structure:
function set.PropName(obj, val)
[doc, val] = obj.setFuncPre('PropName', val);
oldVal = obj.PropName;
obj.PropName = val;
me = obj.setFuncPost('PropName', doc);
if ~isempty(me)
obj.PropName = oldVal;
obj.setFuncRestored('PropName', me);
end
end
Which becomes incredibly long winded when we have hundreds of properties across multiple classes which all have to do the same thing.
So the question is really, is there a way to bypass calling the setter function so that we can use the first example? Or is there a better approach to this?
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!