Property construction methods
VBA provides three kinds of construction methods for properties. You can
choose to use one or all three methods. A property requires at least one of
the methods described in this list:
Let
:
The
Let
method helps you set the value of a property. Use it when
the property has a standard data type as an input value.
Set
:
Use the
Set
method when you create objects that contain other
objects. For example, a
FileSystemObject
contains
Drive
objects.
To access these objects, you must use the
Set
method and not the
Let
method.
When creating an object within an object, you’re essentially creating a
pointer to a location in memory; but unlike in C or other low-level lan-
guages, the location isn’t important. Within the referenced object are
properties that hold values that affect the operation of the object. You
don’t care how the object works with the property. All you know is that
if you set the
Caption
property of a control, you’ll see that text dis-
played somehow onscreen. Other properties work the same way —
they affect the operation of the object in some documented way. The
property is yet another kind of indirect pointer, so you need some way
to dereference that pointer and work with the data that it points to.
That’s where
Let
and
Get
come into play. Use
Let
to set a property in
a referenced object to a specific value. Use
Get
to obtain the value of a
property within the referenced object.
Get
:
The
Get
method returns the stored property value to the caller.
The method that you use for objects is different from other data types,
but you can return any property value by using this method.
When you create a property, you decide not only the name and data type,
but also its accessibility. The accessibility is read only (using the
Get
method), write only (using the
Set
or
Let
method), or read/write (using
the
Get
method along with the
Set
or
Let
method).
Properties have scope, just like everything else in VBA. Besides
Public
and
Private
, a property can also have a special
Friend
scope. The
Friend
scope is a step between
Public
and
Private
. Everything within the current
project can see the property, but nothing outside the current project can.
The
Friend
scope is useful for local configuration. Any part of the local
project — the part of the program that controls the functionality of the
object — can configure the property, but your other projects can’t see it.
You can also add the
Static
keyword to properties. Always add the
Static
keyword when you want the property to maintain its value between calls but
not when you want to make sure that the property resets to the default value.
The
Static
keyword is important when you want the object to maintain its
settings.
187
Do'stlaringiz bilan baham: