Understanding Objects - What objects provide
(Page 2 of 4 )
Objects typically offer three types of data or code to a script. They may use any or all of these three types. They include:
- Methods – These are code procedures that perform specific tasks. They are generally in the form of subroutines and functions.
- Properties – These are in a sense variables. They represent a stored value. This value may be retrieved or set dependent upon whether or not the property is read-only.
- Events – These are special hook and sink strategies that are used to notify other code when a specific event has taken place. Code can be written to react to these events.
These data types must be exposed explicitly by the class in order to be used publicly. Let’s take a look at the Shell Automation Service object as an example. This is a COM object that allows you to interact with the Windows shell. Before you can use it, it must be referenced, or instantiated, in your code. VBScript and Jscript each do this differently.
VBScript:
Set objShell = CreateObject("Shell.Application")
Jscript:
var ShellObject = new ActiveXObject("Shell.Application");
VBScript’s CreateObject function and Jscript’s ActiveXObject function both return an object reference that must be assigned a variable name. You indicate which object you would like to instantiate by specifying its namespace. This is a unique identifier that follows the COM naming convention.
Objects may also contain sub-objects.
Once an object has been instantiated, you can use its reference to access any exposed properties and methods. In OOP, or object-oriented programming, this is done with dot syntax. The remaining examples in this article will be written in VBScript.
Set objShell = CreateObject("Shell.Application")
objShell.MinimizeAll
In the example above we’re calling the MinimizeAll method of the Shell object to minimize all open windows. Notice that dot syntax is being used: object.method.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFso.Drives
intNum = colDrives.Count
In this example, we’re using the FileSystemObject’s Drives property to return a collection of drives. This is a sub-object of the FileSystemObject. We then use its Count property to return the number of sub-objects that it contains.
In VBScript and WSH, some objects do not require instantiation. They are made available by the environment. Two examples are the WScript object, which references the currently active WSH session, and VBScript’s Err object, which references the Standard Error output stream.
Because these objects are made available by the environment, they can be accessed immediately without first connecting to them or assigning them to variables. You can see both in use in the following example.
WScript.Echo WScript.FullName
If Err.number <> 0 Then
WScript.Echo "There was an error."
End If
Notice that there was no need to include any Set statements. Instead, we are able to begin using properties and methods from these objects immediately.
Next: How COM objects are built >>
More Windows Scripting Articles
More By Nilpo