![]() |
Table of Contents |
![]() |
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. PropertiesMost 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. MethodsYou 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. CollectionsSometimes, 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:
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 ObjectIt 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.
In addition, the Tree object has one method.
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 ObjectsYou'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.
|
![]() |
Table of Contents |
![]() |