Chapter 28: Object-Oriented Programming with VBA
987
FIGURE 28.8
Each time you read or write an object’s properties, the class module runs a property procedure.
Consumer Code
Dim Object As ClassType Class Module
Set Object = New ClassType
object.Property = Value
Variable = object.Property
Property Let (Value As DataType)
PropertyVariable = Value
Property Get () As DataType
Property = PropertyVariable
I explain the syntax of each of these procedures in the following sections.
The properties you add to your classes can be read/write, read-only, or write-only, depending on how
you expect the property to be used.
l (^) Read/write: Including both a Property Get and a Property Let (or Property Set)
makes a property read/write. The Property Get lets a consumer read the property’s value,
while the Property Let (or Property Set) lets a value (or object) be assigned to a
property.
l (^) Read-only: Omitting the Property Let (or Property Set for object properties) makes a
property read-only. A consumer can read the property’s value through the Property Get
procedure but can’t assign a new value to the property.
Obviously, because there is no way to assign a value to a read-only property, the class must
provide the read-only property’s value. This is often done by extracting a value from a data-
base, or from the System Registry, or by reading a value from an .ini file or the operating
system. Because a Property Get is a procedure, you can add any logic your class requires
to obtain the property’s value.
l (^) Write-only: Omitting a Property Get makes a property write-only. You may decide to use a
write-only property for sensitive information such as passwords and login identities. Making a
write-only property is an excellent way to preserve the security of sensitive data. Write-only
properties are also used to provide a class with information that it needs to support its activi-
ties, such as a connection string or database name.
Read/write, read-only, and write-only