Chapter 14: Developing Class Modules

 < Day Day Up > 



Overview

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.

What Is an Object?

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.

What Is a Class?

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.

What Are Properties?

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.

Tip 

Adjectives Describe Nouns
You can think of objects as nouns and properties as adjectives, that is, the red flower or the six-foot-tall person. This concept isn’t perfect because some attributes are relatively specific, such as the truck with 55,230 miles, but this idea might be helpful as you begin working with objects.

What Are Methods?

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.

Tip 

Verbs Describe Actions
You can characterize methods as a verb that performs an operation or a task with the properties in the object.

What Are Events?

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 > 



Microsoft Excel 2003 Programming Inside Out
Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))
ISBN: 0735619859
EAN: 2147483647
Year: 2006
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net