[Previous] [Table of Contents] [Next]


In the real world, a car, a house, a book, and a table are examples of objects. Each of these objects has properties that describe it. For example, a car has a color, a height, a weight, and so on. Another aspect of real-world objects is that you can perform actions with them: you can drive a car, clean a house, read a book, and so forth.

In the world of software development, programs can also take an object-oriented approach by defining software entities that have properties and perform actions. For example, a business application might define a customer software object that represents an individual customer. The customer object would contain data that corresponds to real-world customer properties, such as name, address, credit card number, and so on. The customer object would also define actions that are specific to it, such as placing an order or adding itself to the customer database. One advantage of using an object-oriented approach to software development, then, is that it allows developers to write code that closely models real-world problems.

A Simple Object

Let's look at a simple object, a message dialog box (Figure 3-1). A dialog box can be considered an object because it owns data (which it displays) and performs an action (shows information). The object also has several properties that can be manipulated. It has a position and a size, for example. It also has title text, text within the box itself, a background color, and so on.

Figure 3-1 A dialog box, which is an object

The number of properties depends on the object. Also, properties can be visible (exposed to the programmer) or invisible. Many properties of an object are exposed and can be read or set from a script.

The Object Model

An object can contain other objects. For example, the dialog box shown in Figure 3-1 contains two buttons (OK and Cancel) and a question mark icon. These items are subobjects of the dialog box object.

Now let's look at an entire application—Microsoft Word—to understand how it can also be an object, according to the definition of an object as an entity that owns data and performs an action. Word is an object because it has data (documents, words, numbers, formulas, and so forth) and because actions can be performed on the data (opening and closing documents, inserting and deleting text, and so forth).

You can use a hierarchical approach to define a structure for the objects in an entire application, as shown in Figure 3-2. If you intend to use the first word in the first chapter of a document, you can clearly define its position. The Word object is a subobject of the Sentence object, which is a subobject of the Paragraph object, which is a subobject of the Document object, which is loaded in the Application object.

You can refer to this hierarchy in code like this:


In this chain of subobjects, the object names are separated with dots. The leftmost object is the Application object, which is a parent of the Document object, and so on. If you know the object hierarchy, you can access any object by defining its exact position within the object hierarchy, which is known as the object model.

Figure 3-2 An object model


The chain of subobjects shown in the preceding line of code is misleading, however. An application such as Word can load more than one document at a time, and a document typically consists of more than one paragraph. A paragraph generally has more than one sentence, and a sentence contains more than one word.

To be more precise, we can say that a Document object consists of a collection of Paragraph objects, a Paragraph object can consist of a collection of Sentence objects, and so on. Figure 3-3 shows an object model with collections.

Figure 3-3 An object model with collections

A collection includes more than one subobject of the same type. To access a word in a document, you first have to identify a paragraph in the Paragraphs collection, a sentence in the Sentences collection, and so forth.

If we assume that objects in a collection are counted from 0, we can identify the first word in a document (which is the first word in the first sentence in the first paragraph) as follows:


Notice that the object names are plural. The Documents object is a collection of Document objects. You use Documents(0) to indicate that you want the first member (the first Document object) of the Documents collection. The first Document object might consist of several paragraphs, so you use a Paragraphs object, which is a collection of all Paragraph objects in the document. You use Paragraphs(0) to indicate that you need the first Paragraph object in the Paragraphs collection.

In some Microsoft object models (such as Microsoft Word), object names ending with s identify collections and object names without an s ending identify simple objects. You access objects from a root object, which in most cases is the Application object.


Earlier in the chapter, I introduced a dialog box object. A form is a similar object, and it has properties, such as its x,y position. In principle, you can manipulate an object by using its properties. If you change the x,y property, the form window is moved on the desktop. However, you have to know that changing the x,y property of the object changes its position. A much better way to execute a specific operation on an object is to use a method, which frees you from having to know how the object implements its actions.

For example, you use the Move method to move the form's window to a new position. You can use the Show method to create the form window and set the caption, the background color, the position, and the size. Other methods are available for changing an object or its behavior in many other ways. Whether a certain method is supported depends on the object.

A method is simply a function or a procedure call that is associated with a specific object. Also, properties are just variables that belong to the object.

The advantage of using objects is that you don't need to know how they work internally. All you need to know is an object's name and which properties and methods the object exposes. For example, to show a form containing two buttons, an icon, title text, and text within the box itself, you can simply use a (predefined) form object and set the properties for the title text, the inside text, the buttons, and the icon; the object handles the myriad other details needed to create and display the form.

A Practical Example

Let's turn now to Microsoft Windows Script Host (WSH). WSH itself offers a few objects. When a script is executed, WSH exposes the WScript object by default. This object represents the running script, and it supports a number of methods and properties. For example, the WScript object provides the Echo method for displaying dialog boxes within a script. In VBScript, you can write a statement like this to show a simple dialog box:

WScript.Echo "Hello, world"

This statement uses the object name WScript, a dot, and the Echo method. The parameter "Hello, world" contains the message for the dialog box. (In Chapter 6, I'll cover other ways of displaying dialog boxes from scripts.)

The Echo method in the preceding line of code is really nothing more than a simple procedure call in VBScript. (It differs from a conventional procedure call only in that it includes an object name.) Therefore, the syntax of the method call is the same as that for a procedure call.