I do not know what a “computed property” is and why I would want to use one. I could not find any information in the documentation. Can someone point me in the the right direction to get educated on this topic.
Thanks.
I do not know what a “computed property” is and why I would want to use one. I could not find any information in the documentation. Can someone point me in the the right direction to get educated on this topic.
Thanks.
Say you have a class Person
which contains a property DateOfBirth As Date
. However, many times you wish to know how old that person is, thus, you can create a computed property Age As Integer
. In that properties Get
, you would do the math (or compute) the Age and return it as a result.
Another example, say you have a CreditCard
class with two properties: MaxLimit As Currency
and Borrowed As Currency
. From that you could create a property RemainingCreditLine As Currency
which could have both a Get
and Set
method. The Get
method would Return MaxLimit - Borrowed
and the Set
method would Borrowed = MaxLimit - value
. Thus, multiple properties do not have the ability to get out of sync AND when the persons credit rating goes up, you do not have to recompute everything, just update the 1 point of data, MaxLimit
.
In my programming, if I can compute a value based on a stored value, I do not store the value (except for very complex calculations).
Good explanation Jeremy.
One area I use them commonly is to make a property read-only outside the class. Make a private “mValue As Integer” property, and use a Public “Value As Integer” computed property with only a Get handler. That get handler simply returns mValue. It simply ensures nothing outside the class can tinker with the property.
Methods can do this as well, but they do not show up in the debugger. Computed properties will.
And you where to make a computed property act like a normal property, you could make a normal property called ‘mName’, for example and then make a computed property called ‘name’ and then you could write in the Get method of the computed property, ‘return mName’ and in the set property you could write ‘mName = value’ and that would just be like an ordinary property.
Jeremy Cowgar and ThomGrath’s has a more thorough explanation, explaining the use of a computed property.
I also use them to trigger events, e. g. a ValueChanged event (as Xojo has no Watchpoints or similar)
I use the computed property to make shortcuts to other controls, for example, I would write in the Get method of a computed property called ‘SelectedListbox’ something ‘return Listbox(TabPanel1.Value)’. This saves me having to write ‘Listbox(TabPanel1.Value)’ everytime I need to get the selected Listbox, which varies on which tab is selected, instead I can just write ‘SelectedListbox’. Btw the value property of a TabPanel determines which tab is selected.
[quote=24955:@Thom McGrath]One area I use them commonly is to make a property read-only outside the class. Make a private “mValue As Integer” property, and use a Public “Value As Integer” computed property with only a Get handler. That get handler simply returns mValue. It simply ensures nothing outside the class can tinker with the property.
Methods can do this as well, but they do not show up in the debugger. Computed properties will.[/quote]
As an opposing view, I do not use computed properties when it will simply return the value of a private property specifically because both will show up in the debugger, which has no value to me and takes up precious real estate. In cases where I simply have to return the value of an otherwise private property, I use a method.
Another note about computed properties: Beware of side-effects while debugging. Let’s say you have a computed property that increments a private property every time it’s accessed, so the Get looks something like this:
mCounter = mCounter + 1
return mCounter
Because the values of computed properties are determined for display in the debugger, mCounter will increment when you view the object while debugging. In these cases, it’s better to use a method too.
Excellent info - thanks all!!
And for reference, computed properties are described here in the docs:
User Guide Book 1: Fundamentals, Chapter 5: Classes, Section 3: Properties, Methods and Events.