Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


986


Using Property Procedures


The concept of property procedures is fundamental to object-oriented programming. As the name
implies, a property procedure is a VBA procedure that defines a property for a class. Most classes
contain several to many property procedures.

Note
Property procedures are always public by default. Even if you omit the Public keyword, your property proce-
dure is exposed to the other elements of your applications. You should, however, always use the Public key-
word to clarify the property procedure’s scope. It never hurts to be very explicit in your code.


Looking at the types of property procedures
There are three types of property procedures:

l Property Get: Retrieves the value of a property. A Property Get works very much like
any function and follows the same pattern as any VBA function.
l Property Let: Assigns a new value to the property. Property Let works only for simple
data types such as numeric, strings, and date properties.
l Property Set: Assigns a value to an object property. You would use a Property Set for
a property defined as a recordset or other object data type.

The concepts behind property procedures are illustrated in Figure 28.8. Each time your code refer-
ences a property, the class module responds by running the appropriate property procedure.

continued
It’s very difficult to know where a class might be used, and once a class has been distributed,
any changes to the class might break code in many different places without warning.
Sometimes it’s impossible not to change a property’s value or modify a method’s arguments.
As an example, users might require an additional argument to be passed to the SellProduct
method so that shipping charges can be accurately calculated. Unless you take care to pre-
serve backward compatibility, the consumer code referencing the original version of the
SellProduct method is sure to fail.
One technique I recommend to ensure backward compatibility is to duplicate the property or
method, suffixing a numeric value to its name. For example, you might add SellProduct1
to the class module, leaving the unchanged, original SellProduct for older code. New
code will use the updated SellProduct1 to take advantage of the shipping charges
calculation.
Free download pdf