In this part . . .
T
his is the part of the book where you move from
knowing the language to using the language for benefi-
cial work.
In Chapter 8, I show how VBA can use objects to model
program elements. (For example, VBA sees every docu-
ment that you create as an object with its own, unique fea-
tures.) Arrays and collections
in VBA share similar char-
acteristics to real-world collections.
Chapter 8 also shows
you how to sign your work so that others know that the
application is from you and not from someone else.
In Chapter 9, I help you understand the value of the indi-
vidual elements and the collection as a whole. You can’t
perform many tasks with Office without using collections;
this exceptionally useful programming technique appears
everywhere.
In Chapter 10, I demonstrate techniques for using disk
storage. Microsoft considers XML the next storage tech-
nology that everyone will want to use.
In Chapter 11, I describe how XML can benefit you and
how to use it within VBA.
13_046500 pt03.qxp 12/5/06 5:36 PM Page 180
Chapter 8
Object-Oriented
Programming
In
This Chapter
Understanding how classes work
Creating your own classes
Using the classes that you create in a program
Knowing how to handle errors in your classes
Understanding how to create well-constructed classes
Signing your classes
I
f you’ve followed each chapter of the book to this point, you’ve used
objects to perform tasks. For example, forms
and all the controls that
they contain are objects. The Clipboard also requires use of an object. In
short, objects are an essential part of working with VBA because they
reduce the amount of code that you have to write.
In this chapter, I describe the underlying mechanisms behind objects. You
can create your own objects by using VBA to design classes, which are essen-
tially object blueprints. In fact, you can create these objects and share them
with other people who might need to use the same features. Using and creat-
ing objects by designing classes makes your VBA programming experience
better.
Understanding Classes
A
class
is a description of an object: It’s the blueprint
that VBA uses to build
an object when you request one. The class isn’t the object; it’s merely the set
of building instructions for the object. You can use the class to visualize what
the object will look like, but you can’t use a class to perform any tasks. Once
14_046500 ch08.qxp 12/5/06 5:36 PM Page 181
VBA builds the object using the class, it doesn’t look at the class any more
unless it wants to build another object. This differentiation between classes
and objects is important to remember because many information sources
confuse the two concepts. You need to keep them
separate to better under-
stand how objects work. The sections that follow describe classes and
objects in detail.
Understanding object-oriented
programming concepts
At one time, developers had to worry about every variable, construct, and
step in their code. Procedural languages use step-by-step instructions to
tell the computer how to perform a task. Many developers continue to use
procedural languages because they find them easy to use and understand.
Object-oriented programming (OOP) hides implementation
details from the
developer. All the developer needs to know is that an object accomplishes a
specific task; how the object performs the task is up to the object’s developer.
The act of hiding the working details of an object is called
encapsulation.
The idea behind using objects is that you don’t worry about how the informa-
tion you type gets turned into executable code. This concept may sound really
odd to anyone who’s used to working with a procedural language, but that’s
the way it is. I had similar difficulties when I moved from assembler and C
to C++. It took some time for me to learn that although the object code does
get translated into executable code in some way,
the whole reason for using
objects is to create an
abstraction
so that you worry less about the actual
underlying code than you do about the task the object is supposed to per-
form. The object creator takes care of the internal workings of the object.
Using classes also benefits the developer because it’s possible to inherit all
of the features of another class.
Inheritance
is the act of creating a new class
based on the content of a parent class. For example, you might already have
a class called
Dog
and want to
create a new class called
BorderCollie
.
Because a border collie is a kind of dog, you can inherit the feature of the
Dog
class into the new
BorderCollie
class.
Class theory can become quite complex, but you really don’t need to worry
about the complexities when working with VBA. You can find all kinds of
OOP topics online that discuss everything from the intricacies of good class
definition to whether OOP is really better than procedural code. If you really
want to know the
low-level concepts behind OOP, one of the best places to
begin is Wikipedia (
http://en.wikipedia.org/wiki/Object-oriented_
programming
).
182
Do'stlaringiz bilan baham: