|< Day Day Up >|| |
This chapter introduces the concept of classes. In this chapter, you’ll learn about classes, objects and why they aren’t the same thing. You’ll also learn how to construct classes in Microsoft Visual Basic for Applications (VBA), including defining their properties, methods, and events.
An object is a logical representation of a thing. The thing can be a physical entity such as a person, a flower, a machine, or a place. The thing can also be a logical entity such as a report, an order, or a transaction. The thing can also represent something on the computer such as a button, a cell, a worksheet, or a form.
A class is a template from which an object is created. This template includes both variables and code that are wrapped together in a single entity. An object represents a single instance of a class that can be manipulated by your program.
Confused? Think of a class as a data type such as Integer or String. Before you can use an Integer or a String, you must declare a variable of that type. Just as you can have multiple variables defined as Integers or Strings, you can have multiple variables defined as instances of a single class.
Classes are implemented in Visual Basic for Applications through the class module. Each class module holds exactly one class and includes all the properties, methods, and events associated with the class’s interface, along with any other local variables, functions, and subroutines needed to make the class work properly.
Properties are attributes of the thing an object represents. For example, a flower will have attributes such as its name, its color, its size, and so on. A person’s attributes include name, address, ID number, and birth date.
An object can also contain references to other objects. An order might have a reference to a customer, or a form might contain a button. For purposes of this discussion, these object references may also be characterized as properties.
An object can also represent a collection of similar things. For example the Employees object could contain a collection of individual Employee objects.
Adjectives Describe Nouns
Methods are actions that are performed by the object. For example, a report object might include a print method that sends the report to a printer. Another way of thinking about methods is that methods describe the operations performed with the information contained in the properties of the object.
Verbs Describe Actions
Although not strictly a part of the object programming model, events are a useful tool that allow an object to communicate information with the program that created the object. Simply put, an event is a subroutine residing in the program that created the object that’s called by the object, which is called from within the object.
An event is a useful technique that obviates the need for a program to constantly monitor an object for changes. Instead, the object calls the event to inform the calling program of a change in the object’s state.
|< Day Day Up >|| |