Correct way of implementing a getter and setter for a handle class

16 views (last 30 days)
Is it syntactically correct to implement getter and setter methods for a MATLAB handle class the same way as you would do for a value class? I.e. by implementing a
function obj = set.testVar(obj,newVal)
funtion value = get.testVar(obj)
Or is it necessary to always inherit from hgsetget if you want getter/setter functionality for any handle class. The reason I ask is because the above does seem to work, although I do get a "Probable conflict between handle and value class usage", a "The class ClassName is derived from a 'handle' class, so must be a handle class warning" and a "Method set.testVar has a signature consistent with a value class" warning.

Accepted Answer

Daniel Shub
Daniel Shub on 17 Dec 2011
For handle classes it should be
function set.testVar(obj,newVal)
You do not need to return the obj, since you already have the object. For value classes you need to return the object, because the setter in essence creates a new object.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!