Get and Set Methods for Dependent Properties

Dependent properties do not store data. The value of a dependent property depends on other values, such as the values of nondependent properties. Define a dependent property using this syntax:

```properties (Dependent) PropertyName end```

Because dependent properties do not store data, you must define get methods (`get.PropertyName`) to determine the value for the properties when the properties are queried.

Dependent properties can also have set methods (`set.PropertyName`), but these methods cannot actually set the value of the dependent property. However, a set method can contain other code. For example, it can set values of properties related to the dependent property.

For an introduction to defining get and set methods, see Property Get and Set Methods.

Define a Get Method for a Dependent Property

The `Account` class stores an amount in US dollars, and it can return that value in one of three currencies: US dollars, euros, or Japanese yen. That converted value is represented by the dependent `Balance` property. The `get.Balance` method uses `USDollarAmount` and `Currency` to determine a conversion rate to calculate the `Balance` property.

```classdef Account properties Currency {mustBeMember(Currency,["USD","EUR","JPY"])} = "USD" USDollarAmount = 0 end properties (Dependent) Balance end methods function value = get.Balance(obj) c = obj.Currency; switch c case "EUR" v = obj.USDollarAmount/0.98; case "JPY" v = obj.USDollarAmount/0.0069; otherwise v = obj.USDollarAmount; end value = v; end end end```

Create an instance of `Account`. Set the `USDollarAmount` and `Currency` properties.

```a = Account; a.USDollarAmount = 100; a.Currency = "JPY";```

You cannot explicitly call get methods. When you access `Balance`, MATLAB® calls the get method to return the initial amount converted to yen.

`a.Balance`
```ans = 1.4493e+04```

MATLAB also calls get methods when it displays the object. When you set `Currency` to euros without ending the statement with a semicolon, MATLAB calls the `Balance` get method to display the updated value.

`a.Currency = "EUR"`
```a = Account with properties: Currency: "EUR" USDollarAmount: 100 Balance: 102.0400```

When to Use Set Methods with Dependent Properties

Although dependent properties do not store values, you can still define set methods for them. The set methods cannot set the value of a dependent property, but they can execute other code.

For example, `propertyChange` is a value class that initially defined a property named `OldPropName`. You can use a set method to change the property name from the perspective of a class user:

• Redefine `OldPropName` as dependent and hidden.

• Define a new property with the name you would like to replace `OldPropName`.

• Define a set method for `OldPropName` that stores the value in `NewPropName`.

• Define a get method for `OldPropName` that returns the value stored in `NewPropName`.

```classdef propertyChange 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 end```

Code that accesses `OldPropName` continues to work as expected, and making `OldPropName` hidden helps prevent new users from seeing the old property name.

For example, create an instance of `propertyChange`. Set the property value using the old property name and then display the object. MATLAB sets the value to the property with the new name and displays it.

```a = propertyChange; a.OldPropName = "hello"```
```p = propertyChange with properties: NewPropName: "hello"```