Microsoft Office 2000/Visual Basic Programmer's Guide |
A class is a definition for an object. It contains information about how an object should behave, including its name, methods, properties, and events. It's not actually an object itself, in that it doesn't exist in memory. When code runs that refers to a class, a new instance of the class, an object, is created in memory. Although there is only one class, multiple objects of the same type can be created in memory from that class.
You can think of a class as an object "on paper" — that is, it provides the blueprint for an object, but has no substance in memory itself. Any number of objects can be created from this blueprint. A useful analogy is that a class is like a cookie cutter, and the objects are like cookies. Each cookie may have slightly different characteristics, but all are cut from the same cookie cutter. Similarly, each object created from a class has the same members: its properties, methods, and events. But each object behaves as an independent entity; for example, one object's properties may be set to different values than those of another object of the same type.
A VBA project can contain two different varieties of class modules: basic class modules, which don't have any kind of user interface associated with them, and class modules that are associated with a form or another component. Class modules that are associated with a form, for example, are identical to basic class modules, except that they exist in memory only when that form exists in memory. Examples of objects that have associated class modules are UserForms, Access forms and reports, the Microsoft Word ThisDocument object, and the Excel ThisWorkbook and SheetN objects. This chapter will focus on using basic class modules to create objects that don't have an associated component.