Creating the Property Let Procedures

In addition to retrieving values stored in data members (private variables) with Property Get procedures, you must prepare corresponding Property Let procedures to allow other procedures to change the values of these variables as needed. However, you don't need to define a Property Let procedure if the value stored in a private variable is meant to be read-only. Suppose you don't want the user to change the employee ID. To make Id read-only, you simply don't write a Property Let procedure for it. Hence, the CEmployee class will only have three properties (LastName, FirstName, and Salary). Each of these properties will require a separate Property Let procedure.

1. Type the following Property Let procedures in the CEmployee class module:

Property Let LastName(L As String)

m_LastName = L End Property

Property Let FirstName(F As String)

m_FirstName = F End Property

Property Let Salary(ByVal dollar As Currency)

m_Salary = dollar End Property

The Property Let procedures require at least one parameter that specifies the value you want to assign to the property. This parameter can be passed by value (see the ByVal keyword in the Property Let Salary procedure shown above) or by reference (ByRef is the default). If you need a refresher on the meaning of these keywords, see "Passing Arguments by Reference and by Value" in Chapter 4. The data type of the parameter passed to the

Property Let procedure must have exactly the same data type as the value returned from the Property Get or Set procedure with the same name. Notice that the Property Let procedures have the same name as the Property Get procedures prepared in the preceding section. By skipping the Property Let procedure for the Id property, you created a read-only Id property that can be retrieved but not set.

Tip 11-4: Defining the Scope of Property Procedures

You can place the Public, Private, or Static keyword before the name of a property procedure to define its scope. For example:

To indicate that the Property Get procedure is accessible to other procedures in all modules, use the following statement format:

Public Property Get FirstName( ) As String

To make the Property Get procedure accessible only to other procedures in the module where it is declared, use the following statement format:

Private Property Get FirstName( ) As String

To preserve the Property Get procedure's local variables between procedure calls, use the following statement format:

Static Property Get FirstName( ) As String

If not explicitly specified using either Public or Private, property procedures are public by default. Also, if the Static keyword is not used, the values of local variables are not preserved between the procedure calls.

0 0

Post a comment