Set and Get Methods for Dependent Properties

Dependent properties do not store data. The value of a dependent property depends on some other value, such as the value of a nondependent property.

Dependent properties must define get-access methods (get.PropertyName) to determine a value for the property when the property is queried.

The values returned by dependent property get methods are not considered when testing for object equality using isequal and isequaln.

To be able to set the value of a dependent property, the property must define a set access method (set.PropertyName). The property set access method usually assigns the value to another, nondependent property for storage of the value.

For example, the Account class returns a value for the dependent Balance property that depends on the value of the Currency property. The get.Balance method queries the Currency property before calculating a value for the Balance property.

MATLAB® calls the get.Balance method when the Balance property is queried. You cannot call get.Balance explicitly.

Here is a partial listing of the class showing a dependent property and its get method:

classdef Account
   properties
      Currency
      DollarAmount
   end
   properties (Dependent)
      Balance
   end
   ...
   methods
      function value = get.Balance(obj)
         c = obj.Currency;
         switch c
            case 'E'
               v = obj.DollarAmount / 1.1;
            case 'P'
               v = obj.DollarAmount / 1.5;
            otherwise
               v = obj.DollarAmount;
         end
         format bank
         value = v;
      end
   end
end

Calculate Dependent Property Value

One application of a property get method is to determine the value of a property only when you need it, and avoid storing the value. To use this approach, set the property Dependent attribute to true:

properties (Dependent = true)
   Prop
end

The get method for the Prop property determines the value of that property and assigns it to the object from within the method:

function value = get.Prop(obj)
   value = calculateValue;
   ...
end

This get method calls a function or static method called calculateValue to calculate the property value and returns value as a result. The property get method can take whatever action is necessary within the method to produce the output value.

For an example of a property get method, see Calculate Data on Demand.

When to Use Set Methods with Dependent Properties

Although a dependent property does not store its value, you can define a set method for a dependent property to enable code to set the property.

For example, suppose that you have a class that changes the name of a property from OldPropName to NewPropName. You can continue to allow the use of the old name without exposing it to new users. To support the old property name, define OldPropName a dependent property with set and get methods:

properties
   NewPropName
end
properties (Dependent, Hidden)
   OldPropName
end
methods
   function obj = set.OldPropName(obj,val)
      obj.NewPropName = val;
   end
   function value = get.OldPropName(obj)
      value = obj.NewPropName;
   end
end

There is no memory wasted by storing both old and new property values. Code that accesses OldPropName continues to work as expected. Setting the Hidden attribute of OldPropName prevents new users from seeing the property.

Assignments made from property set methods cause the execution of any set methods defined for properties being set. See Calculate Data on Demand for an example.

Private Set Access with Dependent Properties

If you use a dependent property only to return a value, then do not define a set access method for the dependent property. Instead, set the SetAccess attribute of the dependent property to private. For example, consider the following get method for the MaxValue property:

methods
   function mval = get.MaxValue(obj)
      mval = max(obj.BigArray(:));
   end
end

This example uses the MaxValue property to return a value that it calculates only when queried. For this application, define the MaxValue property as dependent and private:

properties (Dependent, SetAccess = private)
   MaxValue
end

Related Topics