2.2 ClassesSince VBScript 5.0, developers have been able to create classes to use in their scripts—a definite step along the road of object-oriented programming in VBScript. Writing classes with VBScript is very similar to writing COM objects with VB. Before we look at writing an actual class, let's go over some of the terminology so we are clear on what we are doing and what we are referring to. A class is simply the template for an object. When you instantiate an object (that is, create an instance of a class) in code, VBScript makes a copy of the class for your use. All objects come from a class. Writing the class is simply a matter of creating a design for the objects that you want to use.
So naturally, it
In VBScript, classes must be created in the scripts where you want to use them or they must be included in the scripts that use them. Since VBScript isn't compiled, unless you use Windows Script Components, you don't have the advantage of being able to write a set of VBScript COM classes that are reusable outside of the scripts in which they're defined or that can be easily accessed by programs and scripts written in other languages. 2.2.1 The Class ConstructYou declare a class using the Class...End Class construct. The syntax of the Class statement is: Class classname
where
classname
is the
Classes can contain
To instantiate an object—that is, to create an instance of your class that you can use in your code—use the following syntax:
where oObj is the name you want to assign to your object variable (it again must follow standard VBScript variable naming conventions), and classname is the name of the class. The statement creates an object reference —that is, the variable oObj contains the address of your object in memory, rather than the object itself. 2.2.2 Class VariablesIn addition to properties, methods (which are either functions or subroutines), and events (which are subroutines), the code inside a Class structure can include variable definitions (but not variable assignments). The variable definition can take any of the following forms: Dim varName1 [, varName2 ...] Private varName1 [, varName2 ...] Public varName1 [, varName2 ...] The variable name must once again follow standard VBScript variable naming conventions.
The
Dim
,
Private
, and
Public
keywords
In general, it is poor programming practice to make a class variable visible outside of the class. There are
2.2.3 Class Properties
Typically, class properties are used to "wrap" the private variables of a class. That is, to change the value of a private variable, the
Read-only properties (which wrap read-only private variables) have only a Property Get procedure, while write-only properties (which are rare) have only a Property Let or a Property Set procedure. Otherwise, properties have a Property Get procedure and either a Property Let or a Property Set procedure and are read-write.
The use of public properties that are available outside of the class to wrap private variables is
Example 2-5. Using properties to wrap private variables
Class Computer
Private modStrType
Private oOS
Public Property Let ComputerType(strType)
modStrType = strType
End Property
Public Property Get ComputerType( )
ComputerType = modStrType
End Property
Public Property Set OperatingSystem(oObj)
Set oOS = oObj
End Property
Public Property Get OperatingSystem( )
Set OperatingSystem = oOS
End Property
End Class
2.2.4 Class Methods
Methods allow the class to do something. There is no magic to methods—they are simply subroutines or functions that do whatever it is you wish for the object to do. For example, if we created an object to represent a laptop computer in a company's inventory, then we would like to have a method that
Example 2-6. Creating a class methodClass LaptopComputer Private modOwner Public Property Let CompOwner(strOwner) modOwner = strOwner End Property Public Property Get CompOwner( ) CompOwner = modOwner End Property Public Function GetOwner( ) GetOwner = modOwner End Function End Class As with properties, you can use the Public and Private keywords to make methods available inside or outside of the class. In the previous example, the method and both properties are available outside of the class because they are declared as Public .
Note that in Example 2-6, the
Property Get
procedure
The GetOwner method is declared as a function because it returns a value to the calling code. You can write methods as subroutines as well. You would do this when the method that you are calling does not need to pass back a return value to the caller. 2.2.5 Class Events
Two events are automatically associated with every class you create:Class_Initialize andClass_Terminate. Class_Initialize is
Set objectname = New classname causes the event to fire. You can use this event to set class variables, to create database connections, or to check to see if conditions necessary for the creation of the object exist. You can make this event handler either public or private, but usually event handlers are private—this keeps the interface from being fired from outside code. The general format of the Class_Initialize event is: Private Sub Class_Initialize( ) Initalization code goes here End Sub The Class_Terminate event handler is called when the script engine determines that there are no remaining references on an object. That might happen when an object variable goes out of scope or when an object variable is set equal to Nothing , but it also might not happen at either of these times if other variables continue to refer to the object. You can use this handler to clean up any other objects that might be opened or to shut down resources that are no longer necessary. Consider it a housekeeping event. This is a good place to make sure that you have returned all memory and cleaned up any objects no longer needed. The format of the Class_Terminate event is: Private Sub Class_Terminate( ) Termination code goes here End Sub Once again, the event handler can either be public or private, though ordinarily it's defined as private to prevent termination code from being executed from outside of the class. |