# Computed Property 101

**URL:** <https://forum.xojo.com/t/computed-property-101/10059>\
**Category:** General\
**Created:** [August 4, 2013, 11:43am UTC](https://forum.xojo.com/t/computed-property-101/10059 "2013-08-04T11:43:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Pastor1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Mark\_Pastor1](https://forum.xojo.com/u/Mark_Pastor1)\
**Post date:** [August 4, 2013, 11:43am UTC](https://forum.xojo.com/t/computed-property-101/10059/1 "2013-08-04T11:43:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [August 4, 2013, 11:56am UTC](https://forum.xojo.com/t/computed-property-101/10059/2 "2013-08-04T11:56:36Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [August 4, 2013, 12:18pm UTC](https://forum.xojo.com/t/computed-property-101/10059/3 "2013-08-04T12:18:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Oliver\_Scott-Brown](https://forum.xojo.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Oliver\_Scott-Brown](https://forum.xojo.com/u/Oliver_Scott-Brown)\
**Post date:** [August 4, 2013, 1:34pm UTC](https://forum.xojo.com/t/computed-property-101/10059/4 "2013-08-04T13:34:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Oliver\_Scott-Brown](https://forum.xojo.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Oliver\_Scott-Brown](https://forum.xojo.com/u/Oliver_Scott-Brown)\
**Post date:** [August 4, 2013, 1:36pm UTC](https://forum.xojo.com/t/computed-property-101/10059/5 "2013-08-04T13:36:03Z")

</div>

> [@24966:@Oliver Scott-Brown](#):
>
> Jeremy Cowgar

Jeremy Cowgar and ThomGrath’s has a more thorough explanation, explaining the use of a computed property.

---

<div class="post-metadata">

**Author:** ![Alex\_von\_Siebenthal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alex\_von\_Siebenthal](https://forum.xojo.com/u/Alex_von_Siebenthal)\
**Post date:** [August 4, 2013, 1:36pm UTC](https://forum.xojo.com/t/computed-property-101/10059/6 "2013-08-04T13:36:32Z")

</div>

I also use them to trigger events, e. g. a ValueChanged event (as Xojo has no Watchpoints or similar)

---

<div class="post-metadata">

**Author:** ![Oliver\_Scott-Brown](https://forum.xojo.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Oliver\_Scott-Brown](https://forum.xojo.com/u/Oliver_Scott-Brown)\
**Post date:** [August 4, 2013, 1:42pm UTC](https://forum.xojo.com/t/computed-property-101/10059/7 "2013-08-04T13:42:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [August 4, 2013, 2:12pm UTC](https://forum.xojo.com/t/computed-property-101/10059/8 "2013-08-04T14:12:06Z")

</div>

[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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Mark\_Pastor1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Mark\_Pastor1](https://forum.xojo.com/u/Mark_Pastor1)\
**Post date:** [August 5, 2013, 9:25pm UTC](https://forum.xojo.com/t/computed-property-101/10059/9 "2013-08-05T21:25:22Z")

</div>

Excellent info - thanks all!!

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [August 5, 2013, 9:52pm UTC](https://forum.xojo.com/t/computed-property-101/10059/10 "2013-08-05T21:52:23Z")

</div>

> [@24950:@Mark Pastor](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 4:47pm UTC](https://forum.xojo.com/t/computed-property-101/10059/11 "2020-10-29T16:47:21Z")

</div>


