Creating Property Procedures

A person's name, height, weight, age, and so on, can all be considered properties of the object known as humans. That is, they are the object's attributes or defining characteristics. In OOP, this definition also holds true of class properties.

In a programming environment, it is unwise to allow a user to change an object's properties without validating the value, a task that is best left in the object's capable hands. Additionally, there may be other actions that need to be taken when a property is changed. It is for these reasons that property procedures were invented.

Property procedures come in three flavors: Property Get, Property Let, and Property Set, and they provide a standardized way of setting and retrieving the object's properties.

Note: Property procedures are the only procedures that can share the same name within the same module.

The Property Get procedure retrieves (or gets) the value of the class's property. Alternatively, the Property Let and Property Set procedures set (or change) their values. The difference between them is that Property Let procedures set scalar values (like integers, strings, and so on), whereas Property Set is used for objects.

In the Kennel class example, mintDogs is the actual property, and the two Dogs methods are its property procedures. The property itself is declared as Private, to ensure that VBA code must access the property through one of the defined property procedures. In this way, your class can always be assured of controlling how dogs are added to the kennel and knowing when a property changes.

0 0

Post a comment