Previous Section Table of Contents Next Section

What Are Objects?

I've already made a big deal about how VBScript lets you access operating system functionality because VBScript is object based, and Windows exposes much of its functionality through objects. So you may be wondering, "What the heck is an object?"

Bear with me for the 10-second synopsis. You may have heard of COM or COM+, two versions of Microsoft's Component Object Model. The whole idea behind COM is that software developers can package their code in a way that makes it easily accessible to other applications. For example, suppose some poor developer spent a few years developing a cool way to interact with e-mail systems. If the developer wrote that code according to the rules of COM, every other developer-or scripter-would be able to take advantage of that e-mail interaction. In fact, a bunch of developers did exactly that! You may have heard of Microsoft's Mail Application Programming Interface, or MAPI. It's what Microsoft Outlook uses to access an Exchange Server, for example. MAPI is an example of COM in action; any programmer-including you-can use MAPI to access a mail server, because MAPI is written to the COM standard.

Therefore, an object is simply a piece of software that's written to the COM standard. VBScript can use most objects that are written to the COM standard; most of Windows' functionality is written to the COM standard, and that's what makes VBScript so powerful.

Properties

Most software requires some kind of configuration to use it, and COM objects are no exception. You configure an object by setting its properties. Properties are simply a means of customizing an object's behavior. For example, an e-mail object might have properties for setting the mail server name, setting the user's name and password, and so forth.

Properties can also provide information to your script. A mail object might include a property that tells you how many new messages are available or how much space is left in the mailbox.

In your scripts, you'll generally use a variable name to represent an object. I use variable names that start with the letter o, so that I know the variable is really an object reference. To refer to an object's properties, simply list the property name after the object name. For example, suppose you have a mail object referenced by the variable oMail. To set the mail server name, you might use oMail.ServerName = "mail.braincore.net". The period in between the object variable and the property name helps VBScript distinguish between the two.

Methods

You already know that functions, statements, and subroutines exist in VBScript. Objects have functions, statements, and subroutines too, but they're called methods. Suppose your fictional mail object provided a statement named GetMail, which retrieved mail from the mail server. You could then simply include oMail.GetMail in your script to activate the statement and retrieve the mail.

Like functions and statements, some methods accept input parameters. For example, oMail.GetMail(1) might retrieve the first message in the mailbox. Other methods might work as functions sMessage = oMail.GetMail(2) might retrieve the second message and store the message body in the variable sMessage.

How do you know what methods an object supports? Check the documentation. Also, I'll introduce you to several useful objects in Chapters 11 and 12.

Collections

Sometimes, programmers create objects that represent a hierarchy of real-world data. One common hierarchy that you're probably familiar with is the file system on a computer: It's a tree of folders and files. If you wanted to manipulate the file system in a script, you'd need an object that represented that hierarchy of folders and files.

COM provides a means for objects to represent hierarchies through collections. A collection is simply a special property that represents several other objects. Sound complicated? It's not! Consider a folder named Test, which contains two files: File1 and File2. Test also contains two subfolders, named Test1 and Test2. Test1 contains a file named FileA.

Now, suppose you've created a theoretical file management object and assigned it to variable oFiles. oFiles might have the following useful properties:

  • A Files property that returns a collection of file objects.

  • A Subfolders property that returns a collection of folder objects.

  • Folder objects that have their own Folders and Files collections.

  • File objects that have properties for FileSize, FileName, and so forth.

How would you find the size of the first file under Test? oFile.Files(0).FileSize. That starts with your oFile object reference, grabs the first file object in the Files collection (most collections start at zero, not one), and then gets that file's FileSize property. Notice the periods separating each portion of the object reference.

How would you get the size for FileA? oFile.Subfolders(0).Files(0).FileSize. You would start with your oFile object reference, move onto the first subfolder, grab the first file in that subfolder, and then get the file size.

NOTE

This file management object isn't actually fictional-Windows includes one, called the FileSystemObject. I'll cover it in Chapter 12.


A Sample Object

It may be easier to see what all of this object stuff is about with a nontechnical sample. Here, I'll break my usual policy of only including useful administrative samples in favor of clarity.

Suppose you're a biology major in college, and you're working with trees. You want to create a computer model of a tree so that you can simulate how it lives in various environmental conditions. You write the computer model to the COM specification, creating a Tree object. The object has the following properties.

  • Species. This read/write property sets or retrieves the species of the simulated tree.

  • Age. This read/write property sets or retrieves the age of the tree in years.

  • Environment. This read/write property sets or retrieves the environment of the tree.

  • Disease. This read-only property retrieves a True or False value, which indicates if the tree has a disease.

In addition, the Tree object has one method.

  • Grow. This method accepts a parameter indicating how many months the tree should grow in the simulated environment.

Listing 5.4 shows a simulated script that uses the Tree object.

Listing 5.4. TreeObject model script. Working with the fictional Tree object.

' Assumes the Tree object is referenced by

' variable oTree.



' set initial parameters

oTree.Species = "Oak"

oTree.Age = 12

oTree.Environment = "City"



' grow the tree

oTree.Grow(36)



' retrieve values

MsgBox "Tree is " & oTree.Species & ", " & _

 oTree.Age & " years old, in " & oTree.Environment

MsgBox "Tree is diseased: " & oTree.Disease

After running this script (if you could, which you can't), you'd get a message box saying "Tree is Oak, 15 years old, in City." A second message box would indicate whether the tree was healthy.

As you can see, the properties, collections, and methods of objects provide a straightforward way to access powerful features.

Scripting with Objects

You've already seen a small version of how to work with objects in script, so it's time for a full example. This is actually a preview of Chapter 12 and uses the FileSystemObject I mentioned earlier.

graphics/arrow.gif Listing Files

Listing 5.5 shows a brief sample script that displays the name of each file in the root of the C: drive.

Listing 5.5. RootFiles. Filenames will be displayed in message boxes.

Dim oFSO, oFile, oFolder

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\")

For Each oFile in oFolder.Files

 MsgBox oFile.Name

Next

graphics/arrow.gif Listing Files-Explained

This script starts with a variable declaration. This might be a new type of declaration for you, because it declares three variables on one line. This functionally is the same as three separate Dim statements, just a bit shorter.


Dim oFSO, oFile, oFolder

' Same as:

' Dim oFSO

' Dim oFile

' Dim oFolder

Next, the script uses the Set statement and CreateObject function to create a reference to the FileSystemObject. CreateObject requires the class name of the object you want; you'll usually get that class name from the documentation for the object. Note that the Set command is required whenever you're assigning an object reference to a variable.


Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder("C:\")

That second line of code uses the FileSystemObject's GetFolder method, which is actually a function. It accepts the name of a folder and returns a folder object that represents that folder. In this case, the object is assigned to the variable oFolder.

The next three lines of text loop through the folder's Files collection, one at a time. For each one, it displays the file's name in a message box.


For Each oFile in oFolder.Files

  MsgBox oFile.Name

Next

If you want to jump ahead and see what For Each is all about, you'll find it in the "For Each" section in Chapter 10.

    Previous Section Table of Contents Next Section