Computed Property... Can you help me to understand

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

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

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 )

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.

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

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

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

I read 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 :

DefaultUser = "Bob"

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

?

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.

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=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 :slight_smile:

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