It helps to look at existing classes when you think about your own class, to
see what other people have done. All the controls provided with VBA make
a good starting point because you can play with them in code to see how
Microsoft designed a particular feature.
You should also examine VBA
objects, such as the ones used in the examples in this book. Control and
component examples are readily available for your use as patterns for your
own class. (See the “Locating Just the Right Code” section of Chapter 17 for
ideas on where to find examples.) Here’s a list of the items that you should
consider copying:
Property, method, and event names:
Use recognizable (similar) names
for
properties, methods, and events. For example, don’t call your
Click
event a
Pushed
event, because no one will know what you mean.
Mandatory features:
It’s easy to forget to include a feature in your class.
Existing classes can help you create a complete list of mandatory features,
such as a
Caption
property, so that nothing is left out.
Design style:
Your control provides unique features and functionality.
However, it helps to look at the way
other people design classes,
especially their visual elements. For example, something as simple as
providing a True/False drop-down list box for Boolean properties is very
helpful.
Visual aids:
A
class can provide visual aids, such as special dialog boxes
for some types of information. Look at other classes for ideas on how
you can add visual aids to your class that make it easier to use.
Privacy:
You might find it tempting to expose every feature to anyone
who might use it. However, that approach
can lead to problems because
objects are only supposed to expose necessary elements. Use other
classes as examples of what you should keep hidden in your own class.
The example in this section shows how to create a simple class. The compo-
nent encapsulates the
MsgBox
function and makes
it easier to use by helping
you see the options clearly. More importantly, the example demonstrates
how to construct properties, methods, and events for this simple class.
Defining
properties
A
property
describes a feature of the object, such as its color, the caption, a
method of presentation (such as sunken or etched),
or some other character-
istic. Don’t assume that a property has to describe a physical characteristic
of the object. A filename is a perfectly acceptable property. A property is a
special kind of object-specific data.
186
Do'stlaringiz bilan baham: