# Computed Property... Can you help me to understand

**URL:** <https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232>\
**Category:** General\
**Created:** [July 29, 2014, 6:33am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232 "2014-07-29T06:33:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergio\_Tamborini](https://forum.xojo.com/user_avatar/forum.xojo.com/sergio_tamborini/32/729_2.png) [@Sergio\_Tamborini](https://forum.xojo.com/u/Sergio_Tamborini)\
**Post date:** [July 29, 2014, 6:33am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/1 "2014-07-29T06:33:01Z")

</div>

Hello all!  
I don’t know what Computed Properties are and I don’t find any help on the language reference and on this forum too…  
Can anyone help me to understand what they are? Any link… any suggestion…

Many thanks

---

<div class="post-metadata">

**Author:** ![Albin\_Kiland](https://forum.xojo.com/user_avatar/forum.xojo.com/albin_kiland/32/1028_2.png) [@Albin\_Kiland](https://forum.xojo.com/u/Albin_Kiland)\
**Post date:** [July 29, 2014, 6:40am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/2 "2014-07-29T06:40:09Z")

</div>

There are some explanations in the User Guide Book 1: Fundamentals on page 139 🙂  
[http://41160df63757fc043cfd-66287f38a83954e31a54d1dbe33e0650.r4.cf2.rackcdn.com/Documentation/EN-PDF/UserGuide-Fundamentals.pdf](http://41160df63757fc043cfd-66287f38a83954e31a54d1dbe33e0650.r4.cf2.rackcdn.com/Documentation/EN-PDF/UserGuide-Fundamentals.pdf)  
Hope it helps.  
Snippet:

> [@](#):
>
> Computed Properties  
> A computed property is a property that does not store its value  
> but instead calculates it each time it is accessed.  
> A computed property has two parts  
> to it: a getter and a setter. This  
> displays in the Navigator as a Get  
> and Set node below the property  
> name. You add code to the Get  
> node to get a value for the property.  
> You add code to the Set node to set  
> a value for the property. If you do  
> not include code in Set then the  
> property becomes a read-only property.

---

<div class="post-metadata">

**Author:** ![Markus\_Winter](https://forum.xojo.com/user_avatar/forum.xojo.com/markus_winter/32/144_2.png) [@Markus\_Winter](https://forum.xojo.com/u/Markus_Winter)\
**Post date:** [July 29, 2014, 6:45am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/3 "2014-07-29T06:45:38Z")

</div>

Let us say you hava a normal property. You can access it and get the value, or you can set the value.

Now there are situations where changing one property changes other properties too.

Let’s say you have properties .text and .length in your class.

Now when you change .text then you have to remember to also change .length.

Very easy to forget and therefore error prone.

However if the property .text could automatically update the property .length then you don’t need to remember anything.

A computed property allows you to do just that.

How does it work?

It uses two methods which receive the value and are executed when the property is set or read respectively.

Now methods have nowhere to store a value, so you also need a place to store the value. By tradition it is the variable name preceded with an m.

To turn a property into a computed property righ-click on it and select make computed property.

In our example:

In the method .text.set write

Me.Length = Len( value )

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 29, 2014, 7:11am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/4 "2014-07-29T07:11:07Z")

</div>

One significant difference between a computed property and a getter/setter pair of methods is that a computed property will be displayed in the debugger - you can automatically see the getter’s value.

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [July 29, 2014, 12:10pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/5 "2014-07-29T12:10:04Z")

</div>

Also, it’s worth noting that a computed property allows you to create read-only or write-only properties by deleting the code in the setter or getter method (respectively).

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [July 29, 2014, 12:26pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/6 "2014-07-29T12:26:05Z")

</div>

[code]Class Class1

Sub DoCalcs() // Example of the behavior of a computed property  
R=C // R = C.Get() = A+B = 1+1 = 2 =\> R=2  
C=1 // C.Set(1) =\> (B=1\*3) =\> (B=3)  
A=2  
R=C // R = 2+3 = 5  
End Sub

// R, A and B are normal integer properties

Property R As Integer = 0  
Property A As Integer = 1  
Property B As Integer = 1

// C is a computed property. It emulates an usual property calling automatically internal methods: Get()/Set(value)

Computed Property C As Integer

Function Get  
Return A + B // \<-- You wrote this behavior  
End

Sub Set(value)  
B = value \* 3 // \<-- You wrote this behavior  
End

End Property

End Class[/code]

---

<div class="post-metadata">

**Author:** ![Sergio\_Tamborini](https://forum.xojo.com/user_avatar/forum.xojo.com/sergio_tamborini/32/729_2.png) [@Sergio\_Tamborini](https://forum.xojo.com/u/Sergio_Tamborini)\
**Post date:** [July 30, 2014, 6:08am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/7 "2014-07-30T06:08:59Z")

</div>

Many thanks to all! It seems more clear now.  
Thanks for the examples too! It’s very helpful!

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 30, 2014, 6:57am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/8 "2014-07-30T06:57:42Z")

</div>

I read [http://www.xojo.com/blog/en/2013/09/using-computed-properties-and-method-assignment.php](http://www.xojo.com/blog/en/2013/09/using-computed-properties-and-method-assignment.php) and tried the example shown

`Private Property DefaultUser As String
Get
 Return mDefaultUser
End Get
Set
 mDefaultUser = value
 DefaultUserLabel.Text = value
End Set
`

I added the computed property to the main window, but when I go in a button added to the window in its action event :

```auto
DefaultUser = "Bob"
```

I get the compile error “Cannot assign a value to this property”.

?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [July 30, 2014, 8:20am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/9 "2014-07-30T08:20:15Z")

</div>

Because it’s private? mDefaultUser should be private. DefaultUser should not.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 30, 2014, 9:06am UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/10 "2014-07-30T09:06:06Z")

</div>

> [@115965:@Tim Hare](#):
>
> Because it’s private? mDefaultUser should be private. DefaultUser should not.

DefaultUser is public. But if I set it to private I get the same error.

If instead of a computer property I declare a regular string property with the same name, no error.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [July 30, 2014, 10:19pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/11 "2014-07-30T22:19:32Z")

</div>

On a window add a label - DefaultUserLabel  
To the window add a private regular non-computed property - mDefaultUser as string  
To that same window add a computed property DefaultUser as string  
With the following code

`Get
 Return mDefaultUser
End Get
`

`Set
 mDefaultUser = value
 DefaultUserLabel.Text = value
End Set`

now on that window you can set the DefaultUser property

from outside the window you an set default user but NOT mDefaultUser

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 30, 2014, 10:57pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/12 "2014-07-30T22:57:30Z")

</div>

[quote=116176:@Norman Palardy]On a window add a label - DefaultUserLabel  
To the window add a private regular non-computed property - mDefaultUser as string  
To that same window add a computed property DefaultUser as string  
With the following code

`Get
 Return mDefaultUser
End Get
`

`Set
 mDefaultUser = value
 DefaultUserLabel.Text = value
End Set`

now on that window you can set the DefaultUser property

from outside the window you an set default user but NOT mDefaultUser[/quote]

So what was missing from what I had done was the mDefaultUser as string window private property …

Thank you Norman. I see better, now 🙂

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [July 30, 2014, 11:24pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/13 "2014-07-30T23:24:31Z")

</div>

a computed property is not radically different than a getter setter pair of methods  
Say something like

[code] sub DefaultUser(assigns newValue as string)  
end sub

function DefaultUser() as string  
end function  
[/code]

The BIGGEST differences are

- a computed property is NOT virtual (where a getter setter pair could be overridden in a subclass)
- a getter / setter pair of methods is NOT visible in the debugger (where a property is)

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [July 30, 2014, 11:49pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/14 "2014-07-30T23:49:16Z")

</div>

[quote=116188:@Norman Palardy]a computed property is not radically different than a getter setter pair of methods  
Say something like

[code] sub DefaultUser(assigns newValue as string)  
end sub

function DefaultUser() as string  
end function  
[/code]

The BIGGEST differences are

- a computed property is NOT virtual (where a getter setter pair could be overridden in a subclass)
- a getter / setter pair of methods is NOT visible in the debugger (where a property is)[/quote]

Great. Thanks.

---

<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, 5:21pm UTC](https://forum.xojo.com/t/computed-property-can-you-help-me-to-understand/18232/15 "2020-10-29T17:21:07Z")

</div>


