Property Get Procedures
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
A Property Get procedure returns the value of a property through the procedure's return value. Note that the Property Get procedure complements the Property Let procedure: The Property Get procedure has a return value, but takes no argument. Note also that the argument passed to the Property Let procedure must have the same data type as the return value of the Property Get procedure.
To return a property value by using a Property Get procedure, you must retrieve the value for the property from within the Property Get procedure. Assuming that the corresponding Property Let procedure has already stored the value assigned to the property in a module-level variable, you would read the value of this variable from within the Property Get procedure, and assign it as the return value for the Property Get procedure. For example, the following Property Get procedure is the companion procedure to the Property Let LastName procedure shown in the previous section. It retrieves the value of the p_strLastName
variable and assigns it to the name of the property:
Public Property Get LastName As String
' Retrieve value stored in module-level variable.
LastName = p_strLastName
End Property
When you work with the Customer object from code in another module, you can retrieve the value of the LastName property as follows:
Dim cstCust As Customer
Set cstCust = New Customer
Debug.Print cstCust.LastName
Note If the property has not already been set, the
p_strLastName
variable does not yet have a value; therefore, the Property Get procedure for the LastName property returns an empty string.
In the Property procedures for the LastName property, you can see that both procedures use the p_strLastName
variable. The Property Let procedure writes a value to the variable, and the Property Get procedure reads the value from it.
There might be situations in which you must ensure that a Property Get procedure returns the most up-to-date information from the system or application. In this case, you do not want to store the property value in a variable. Instead, you should call a function that always returns current information.
For example, the Property Get Minute procedure is the companion procedure to the Property Let Minute procedure shown in the previous section. If you stored the value set by the Property Let Minute procedure in a variable, and then used the Property Get procedure to retrieve the value several minutes later, it would be out-of-date and incorrect. Instead, you can call the Windows API GetLocalTime function from within the Property Get procedure to retrieve the current local time for the system, and then return the minute portion, as shown:
Public Property Get Minute() As Integer
' Retrieve current time, then return minute.
GetLocalTime sysSystemTime
Minute = sysSystemTime.wMinute
End Property
See Also
Why Build Your Own Objects? | Basic Class Concepts | Creating Property Procedures | Creating Events and Event Procedures | Extending Objects Through Interfaces | Designing Object Models | Creating Custom Objects for Web Pages