VB6 provides a limited degree of
object-oriented programming. The objects you define can have
properties which are, in essence, data storage locations within the
object. Most properties are variables and are usually defined using
the Property Let and Property Get procedures within the class
definition. However, many programmers don’t know that a property
can also be an object reference. In other words, one object can
contain a reference to another object as a property.
This approach can be very useful. For instance,
suppose you’re writing an application that creates financial
charts. You might define classes for the X-axis, the Y-axis, the
title, and so on. Then, you could define a “Chart” class that
encapsulates all of the individual parts such as the axes and
titles. Clearly, the Chart object will need a way to reference
these other objects.
You’ll treat properties that are object
references a little differently from other properties. This is
because of the VB syntax requirement that object reference
assignments use the Set keyword. Thus, you cannot write simply: Obj = New SomeClass. You
have to write: Set obj = New
SomeClass.
Due to this requirement, the procedure you use
in the class to assign a value to the property isn’t a Property Let
procedure but rather a Property Set procedure. To read the property
value, you still use Property Get. The Property Set procedure looks
like this:
Property Set PropertyName(o as Type)
Set PropertyVar = o
End Property
In this example, Type is either the specific
type of the property or the generic object type Object. PropertyVar
is the internal class variable that is used to hold the property
value.
Having your program’s objects reference other
objects can be a useful technique, and it’s easy if you know how to
use Property Set procedures.
Advance your scripting skills to the next level with TechRepublic’s free Visual Basic newsletter, delivered each Friday. Automatically sign up today!