The termused to define
this relationship to the object is encapsulation.
Encapsulation can be defined as the capability of an object to conceal its inner workings
from client code or other objects. It is one of the fundamental principles of object-oriented programming
(OOP). If an object has clearly defined properties and methods, it will be easily
reusable and will require limited (if any) documentation. When we look at the ADO recordset
object, we can easily understand what its Open or AddNew methods do for us with no concern
for how they provide their services. Your objects will be as well defined as any of the Visual
Basic objects, and therefore easy for you or anyone else to implement in their applications.
Class modules contain only code??”there is no visual interface. Classes you create in Excel
VBA are easily portable to other VBA applications, and can be placed into Visual Basic 5 or 6
code with no (or minimal) modifications and compiled into ActiveX DLLs or EXEs. This allows
your objects to be used in applications outside of Excel.
Use of classes allows for the design of robust, reusable objects. It requires more forethought
and planning, but you receive the benefits of code that is usually more reliable and
easier to maintain.
Class modules are inserted into your project by choosing Insert ?¤ Class Module or by
right-clicking an object in the Project Explorer and choosing Insert ?¤ Class Module from the
pop-up menu.
CHAPTER 1 n THE MACRO RECORDER AND CODE MODULES 30
Sample Class and Usage
Let??™s re-create the Employee user-defined data type that we looked at in a previous example as
an object.
Pages:
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57