Part IV: Professional Database Development
990
In a VBA project, property-value persistence is mediated through private variables contained
within the class module. Generally speaking, each property is accompanied by a private variable
that is the same data type as the property. This means that a property that reads or writes a string
value will be accompanied by a private string variable, and each date property will be accompanied
by a private date variable.
As you saw in the previous section, the property variables are either assigned or returned by the
property procedures. A property variable should be given a name that indicates which property
owns the variable. In the examples accompanying this chapter, each property variable has exactly
the same name as its property, and is tagged with an m_ prefix. For example, the property variable
for the CustomerID property is named m_CustomerID. Furthermore, because the
CustomerID property is a string, m_CustomerID is also a string.
There are cases, of course, where a property is not accompanied by a variable. For example, a read-
only property may extract the value from a database file or retrieve it from the operating system.
Or, the property might be write-only, in which case the property may act immediately on the value
passed to the property procedure, and no storage is necessary.
Heeding property procedure rules
Two rules apply to property procedures:
l (^) The name assigned to a property procedure is the name of the property. Therefore,
you should use a descriptive, helpful name for all your properties. Typically, a developer
using objects created from a class you create doesn’t have access to the VBA code in the
class and has to rely on the names you’ve assigned to its properties and methods for
guidance.
l The data type of the Property Let, Property Get, and the private variable must coin-
cide. For example, if the property is defined as a string, the private variable must be a
string. Figure 28.9 illustrates this concept.
Note the following points in Figure 28.9:
l The property variable is declared as some data type (labeled “A” in Figure 28.9).
l (^) The argument to the Property Let procedure is the same data type as the property
variable (“B” in Figure 28.9).
l (^) The property variable is assigned its value in the body of the Property Let (“C” in
Figure 28.9).
l (^) The Property Get procedure returns the same data type as the property variable
(“D” in Figure 28.9).
l (^) The Property Get is assigned the value of the property variable (“E” in Figure 28.9).
You’ll get the following error if the data type assigned by the property procedures does not
coincide: