Using Property

The Property Set procedure is very similar to Property Let, in that it sets the value of properties. But where Property Let populates scalar properties (integer, date, string, and so on), Property Set populates object properties; that is, properties that are actually pointers to other objects.

For example, in the following clsKennel class module, the Property Set procedure sets the value of the Gate property so that the Property Get procedure can return a new Gate object.

For clarity, we've removed the other properties and methods.

Option Compare Database Option Explicit

'Private variable that will contain a reference 'to an instance of the clsGate object. Private mobjGate As clsGate

Public Property Get Gate() As clsGate

'Return an instance of the mobjGate object that 'was instantiated by the Property Set procedure Set Gate = mobjGate End Property

Public Property Set Gate(objGate As clsGate)

'Instantiate the module-level object variable 'using the object passed to the procedure Set mobjGate = objGate End Property

To use this construct, external VBA code must pass the clsGate object to the Property Set procedure in a Set statement, after which it can access its properties and methods through myKennel's Gate property.

Set myKennel.Gate = New clsGate myKennel.Gate.Color = "Red" myKennel.Gate.Paint

Although Gate is a property of the myKennel object, it has been instantiated as a clsGate object in its own right. Since clsGate has its own properties and methods, they can now be accessed through the object chain just created. This facility allows us the ability to create a basic relational object model.

0 0

Post a comment