Shortcut Menu

Skip

Main Navigation

Choose your language

You are here:

ActiveXperts.com > Scripting Component > Creating Classes with VBScript

ActiveXperts Scripting Component Invoke VBScript functions and remote Windows commands from any application

Quicklinks


Creating Classes with VBScript

Classes aren't a new concept in scripting. JavaScript, JScript, and other scripting languages have supported classes or similar elements for years. However, VBScript 5.0 is the first version of VBScript to support classes.

To use classes in your VBScript code, you first need to obtain VBScript 5.0 by downloading the appropriate self-executable file from the Microsoft Developer Network (MSDN) Web site (http://msdn.microsoft.com/scripting) or by installing Microsoft Internet Explorer (IE) 5.0. Then you need to understand what a VBScript class is and learn how to declare, define, initialize, and instantiate a class.

Understanding VBScript Classes

VBScript 5.0 supports two types of objects: COM objects and Class objects (typically referred to as simply classes). VBScript COM objects have basic subtypes, such as an Integer or String. VBScript classes have an abstract subtype that encapsulates data and the functions to work with that data. You can think of a VBScript class as having a souped-up subtype that provides you with more computing power and flexibility. (Other differences exist between these two types of objects. For more information, see the Web-exclusive sidebar "How VBScript Classes and COM Objects Differ" on the Win32 Scripting Journal Web site at http://www.winntmag.com/ newsletter/scripting.

You can use classes to describe complex data structures. For example, if your application tracks customers and orders, you can define two classes for them, each with a unique set of internal data (typically called properties) and functions (typically called methods). You can then manage customers and orders as if they were native VBScript subtypes. More important, because you assign a class its properties and methods (i.e., its programming interface), you have an object-oriented tool to improve VBScript applications.

Declaring a Class

You use the Class statement to declare a class. This statement's syntax is:

Class name
' Properties and methods go here.
End Class

where name is the name you give that class. You declare the properties and methods for your class between the Class and End Class clauses.

For example, suppose you want to create the VBScript class FileList, which Listing 1 contains. This class manages those files in a folder that meet a filename specification that you provide. You create this class by first specifying the keyword Class followed by the class' name Class FileList. Next, you declare the class' properties and methods. FileList has two properties (FileSpec and FolderPath) and one method (Search).

Declaring the FileSpec Property

The FileSpec property holds the filename specification. For example, the filename specification might be C:\GetAdsiDatabase. You want users to be able to freely read and write values to this property, so you declare FileSpec as an external, or public, variable with the Public statement

Public FileSpec

You can use a public variable in any script, not just the script in which you created the variable. However, if you use a public variable, you have no control over the value that users assign to the variable and no control over the value that the variable returns. Thus, you can't use public variables to hold values that you need to validate.

Declaring the FolderPath Property

The FolderPath property holds the full path to the folder containing the files. After a user sets a folder path, you need to validate that the folder exists, which means you can't use a public variable. Instead, you need to store the folder path in an internal, or private, variable and use two public property procedures to read and write to that variable. (Public property procedures are wrappers that hide the code that gets and sets the values of private variables.)

Prefixing a private variable with the m_ string is a common scripting convention. For example, the private variable for the FolderPath property is m_folderPath. To declare m_folderPath, you use the Private statement

Private m_folderPath

Procedures and variables that have the Private qualifier aren't visible outside the class. In addition, private variables apply only to the script in which you created them.

After you declare m_folderPath, you need to declare the two public property procedures that you'll use to read and write to that variable. The first procedure to declare is the Property Get procedure, which returns the values of properties. The second procedure is the Property Let procedure, which assigns values to properties.

To declare the Property Get procedure, you use the Property Get statement

Public Property Get FolderPath
  FolderPath = m_folderPath
End Property

where FolderPath is the name of that procedure. By including the Public statement with the Property Get statement, you're making the value that the FolderPath procedure returns available for public reading. Thus, by assigning FolderPath to m_folderPath, you make the value of m_folderPath available for public reading.

To declare the Property Let procedure, you use the Property Let statement

Public Property Let FolderPath(path)
  m_folderPath = path
  ' Validation statements go here.
End Property

where FolderPath is the name of that procedure and path is the name of the variable that holds the value the FolderPath procedure returns. You can use the same name for the Property Let procedure as you used for the Property Get procedure as long as both procedures are part of the same Class statement. (You can also use the same name for Property Set procedures, which I discuss shortly.) Using the same name for two procedures in scripts is an exception to VBScript's standard naming convention. After naming the procedure, you assign m_folderPath to path to make the m_folderPath available for public writing.

Within the Property Let procedure, you can add other code to perform tasks, such as validating that the folder exists. You can even add code to make a Property Let procedure return different values for the same property based on the privileges of the user calling that procedure.

If a property holds an object, you can't use Property Let and you must adapt the code for Property Get. Microsoft designed Property Let to work only with basic subtypes, such as Integer, Long, and String. You must use Property Set to work with objects. Property Set and Property Let are mutually exclusive; if you use the wrong one, you receive a runtime error. The syntax for Property Set is similar to that for Property Let

Public Property Set name(reference)
  ' Statements go here.
End Property

where name is the name of the Property Let procedure and reference is the name of the variable that contains the object reference.

If you use Property Get with a private variable that contains an object, you need to add the Set statement because VBScript doesn't let you use the assignment (=) operator to assign objects. For example, if you were using the FolderPath procedure to return a private variable that holds an object rather than a value, you would specify

Public Property Get FolderPath
  Set FolderPath = m_internalObject
End Property

Declaring the Search Method

The Search method is the core of the FileList class. This method retrieves a list of filenames from a given folder and makes the files that meet a given file specification available to the user. To declare the Search method, you use the Public and Function statements

Public Function funcName
  ' Statements go here.
End Sub

where funcName is the name of the method (e.g., Search). You place the code that will perform the method's task within the body of the Function statement. For example, callout A in Listing 1 highlights the code that performs the task of searching folders for files that match the filename specification. You begin this search code by creating an instance of the Microsoft Scripting Runtime library's FileSystemObject (FSO) object. You use the FSO object's GetFolder method to return a given folder. In this case, GetFolder returns the folder that m_folderPath specifies. GetFolder's Files property points to a collection of all the files within that folder.

To extract those files that match the filename specification, you use a For Each...Next statement to loop through the collection and pass each filename to the IsLike function. This user-defined function matches strings against a given pattern to determine whether a filename meets the specification.

Initializing the Class and Its Properties

After you create your class, you need to initialize it so that the client application allocates storage space for that class. You also need to initialize the class' properties so that they have default values in case the client application doesn't assign any. You can use the Initialize event to simultaneously initialize a class and its properties. This event's syntax is

Private Sub Class_Initialize
  ' Statements go here.
End Sub

where Class is the name of the procedure to initialize. In the statements area, you can include the code to initialize the properties or other code you want to run when the client application initializes the class.

For example, to initialize the FileList class and its properties, you use the code at callout B in Listing 1. The statements in this Initialize event set the class to work on C:\GetAdsiDatabase by default. If you initialize a property that holds an object, you need to free the allocated space when you're finished using the class. You use the Terminate event to terminate that instance of the class. The Terminate event's syntax is

Private Sub Class_Terminate
  ' Statemkents go here.
End Sub

where Class is the name of the procedure to terminate.

Creating an Instance of a Class

After you've declared and defined your class, you can use it in the same way you use any other object. For example, you can assign instances of classes to variables. First, you declare the variable that will hold the instance. Then, you use the Set statement with the New keyword to assign an instance of the class to the variable. For example, to create an instance of the FileList class, you specify

Dim obj
Set obj = New FileList

Creating Classes Is Only the Beginning

VBScript's success is leading Microsoft to improve the structure of that scripting language. One such improvement is VBScript 5.0's support for classes. You can use classes to create objects that more closely match the real entities in your domain.

To create a class, you need to declare, define, and initialize the class and its properties and methods. After you create an instance of that new class, you can use that instantiated object not only in the script in which you created the class but also in other scripts and applications.