Sometimes you need a property that does more than simply store a value. It may also need to calculate a value or perform some other action to lookup a value. You can do this using a Computed Property or an Assignment Method. Read on to learn how these work and when to use them.
Computed Properties are properties that do not actually refer to a specific value. Instead they contain code (for one or both of the Get and Set of the property). This code is run when you use the property.
In its simplest form a computed property can simply Get and Set the value of a related private property like this:
Private Property DefaultUser As String Get Return mDefaultUser End Get Set mDefaultUser = value End Set
This computed property is simply getting and setting the value for the private mDefaultUser property. But that is not very useful.
Instead suppose that when you set the DefaultUser property, you also wanted to populate a Label on the layout? You could add that code to the Set section:
Private Property DefaultUser As String Get Return mDefaultUser End Get Set mDefaultUser = value DefaultUserLabel.Text = value End Set
Now it is starting to be more useful. You would use the property like any other property, but it will end up setting the Label text:
DefaultUser = "Bob"
Computed Properties also do not need to have both a Set and a Get. To create a “read-only” computed property, only put code in the Get section. For example, this code can be used to get the amount of time a user has been logged in (notice there is nothing in the Set section):
Private Property SecondsOnline As Double Get Dim now As New Date Dim secs As Double // mLoginTime is a Date that was initialized when user logged in secs = mLoginTime.TotalSeconds - now.TotalSeconds Return secsEnd GetSet End Get
You can use this property to get the value like this:
Dim online As Double = SecondsOnline
Attempting to assign a value to SecondsOnline will cause a compile error.
You can simulate Computed Properties using a set of two methods. The first method simply returns the property value. The second method uses the Assigns keyword so that you can write using the “=” to make it look like a property. For example, this is how DefaultUser could be written as a method:
Private Sub DefaultUser(Assigns value As String) mDefaultUser = value DefaultUserLabel.Text = value End Sub
The Assigns key can only be used for the last parameter. A method written like this is called like a property:
DefaultUser = "Bob"
Which Should I Use?
Why would you use methods instead of computed properties? Or vice-versa? Because of these differences:
- Computed properties appear in the debugger. Methods do not appear in the Debugger.
- This is subject to opinion, but I think computed properties are better organized in the Navigator.
- Methods can be overwritten by inheritance. Computed properties cannot be overridden.
- Methods can be included on Interface declarations. Computed properties cannot.
- Methods can take parameters. Computed properties cannot.
Choose what works for your project. I find I tend to use computed properties most of the time. The good news is that if you start with a computed property and find you later need to change it to a set of methods (or vice-versa), you can do so without affecting any other code that was using the property; it won’t know the difference.