Microsoft Office 2000/Visual Basic Programmer's Guide |
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 hasn't already been set, the p_strLastName
variable doesn't 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 may be situations in which you need to ensure that a Property Get procedure returns the most up-to-date information from the system or application. In this case, you don't 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
This Property procedure is available in the System class module in System.xls, which is available in the ODETools\V9\Samples\OPG\Samples\CH10 subfolder on the Office 2000 Developer CD-ROM. To see how the Property procedure works, run the DisplayLocalSystemTime procedure in the modSystemInfo module in the sample project. This procedure creates a new instance of the System class and displays a dialog box that shows the local system time.