Defining the Properties for the Class

Declaring the variables with the Private keyword guarantees that the variables cannot be accessed directly from outside the object. This means that the VBA procedures from outside the class module will not be able to set or read data stored in those variables. To enable other parts of your VBA application to set or retrieve the employee data, you must add special property procedures to the CEmployee class module. There are three types of property procedures:

■ Property Let—This type of procedure allows other parts of the application to set the value of a property.

■ Property Get—This type of procedure allows other parts of the application to get or read the value of a property.

■ Property Set—This type of procedure is used instead of Property Let when setting the reference to an object.

Property procedures are executed when an object property needs to be set or retrieved. The Property Get procedure can have the same name as the Property Let procedure. You should create property procedures for each property of the object that can be accessed by another part of your VBA application.

The easiest of the three types of property statements to understand is the Property Get procedure. Let's examine the syntax of the property procedures by taking a closer look at the Property Get LastName procedure. The property procedures contain the following parts:

■ A procedure declaration line that specifies the name of the property and the data type:

Property Get LastName ( ) As String

LastName is the name of the property, and As String determines the data type of the property's return value.

■ An assignment statement similar to the one used in a function procedure:

LastName = m_LastName

LastName is the name of the property, and m_LastName is the data member variable that holds the value of the property you want to retrieve or set. The m_LastName variable should be defined with the Private keyword at the top of the class module.

If the retrieved value is obtained as a result of a calculation, you can include the appropriate VBA statement:

Property Get Royalty()

Royalty = (Sales * Percent)-Advance End Property

■ The End Property keywords that specify the end of the property procedure

Tip 11-3: Immediate Exit from Property Procedures

Just like the Exit Sub and Exit Function keywords allow you to exit early from a subroutine or a function procedure, the Exit Property keywords give you a way to immediately exit from a Property procedure. Program execution will continue with the statements following the statement that called the Property Get, Property Let, or Property Set procedure.

0 0

Post a comment