Executing Code when Setting Properties
You can create Property Let, Property Set, and Property Get procedures that share the same name. By doing this, you can create a group of related procedures that work together. Once a name is used for a Property procedure, that name can’t be used to name a Sub or Function procedure, a variable, or a user-defined type.
The Property Let statement allows you to create a procedure that sets the value of the property. One example might be a Property procedure that creates an inverted property for a bitmap on a form. This is the syntax used to call the Property Let procedure:
Form1.Inverted = True
The actual work of inverting a bitmap on the form is done within the Property Let procedure:
Private IsInverted As Boolean
Property Let Inverted(X As Boolean)
IsInverted = X
If IsInverted Then
…
(statements)
Else
(statements)
End If
End Property
The form-level variable IsInverted
stores the setting of your property. By declaring it Private, the user can only change it only using your Property Let procedure. Use a name that makes it easy to recognize that the variable is used for the property.
This Property Get procedure is used to return the current state of the Inverted
property:
Property Get Inverted() As Boolean
Inverted = IsInverted
End Property
Property procedures make it easy to execute code at the same time the value of a property is set. You can use property procedures to do the following processing:
- Before a property value is set to determine the value of the property.
- After a property value is set, based on the new value.