Importing and Porting Classes in VBScript 5.0

Until recently, the only way you could write reusable VBScript code was to import external files through VBScript's Include function or through parser-specific features. Today, you have the elegant yet powerful option of using VBScript classes.

Dino Esposito

December 17, 1999

3 Min Read
abstract gear shapes
Alamy

Until recently, the only way you could write reusable VBScript code was to import external files through VBScript's Include function or through parser-specific features, such as the server-side include (SSI) directive of Active Server Pages (ASP) or the source (SRC) attribute of blocks in HTML. Today, you have two elegant yet powerful options: Windows Script Components (WSC) and VBScript classes.

Windows Script Components (WSC)

WSC leverages a standard, fixed binary module that hard-codes a few COM interfaces. A client module connects to this engine and asks for the functions the COM interfaces provide. The code that executes isn't binary code but rather interpreted VBScript (or JScript) code. Apart from this layered internal architecture, each component in WSC is a typical COM object. (I'll discuss WSC in a future article. If you don't want to wait, you can read "ASP and Windows Script Components," Microsoft Internet Developer, December 1999, or Andrew Clinick, "Windows Script Components—They Get Around" .)

VBScript Classes

VBScript classes are ASCII modules that VBScript internally implements as COM objects. Like WSC, you can use VBScript classes to invoke methods and properties. Unlike WSC, VBScript classes don't support events. However, you can use pseudoevents in classes. The sidebar "A Workaround for Events," page 2, explains how.

Another difference between VBScript classes and WSC is that VBScript classes offer better performance at creation time. Instantiating a class is slightly faster than instantiating a component because you use different instantiation approaches. To instantiate a component, you must use the CreateObject method. This method requires the language parser to pass through the system's Registry and the COM infrastructure, which adds processing time. To instantiate a class, you use the New keyword, which Microsoft added in VBScript 5.0. This keyword doesn't require the language parser to pass through the system's Registry and the COM infrastructure, so VBScript classes enjoy the advantage of better performance.

To reuse VBScript classes, you can import them with Windows Scripting Host (WSH) 2.0 or with WSH 1.0 and a VBScript 5.0 runtime evaluation function. You can also reuse VBScript classes by adapting, or porting, them into Visual Basic (VB) code.

Importing VBScript Classes

With WSH 2.0

After you create a VBScript class, you can import that class across client files and applications. (If you're unfamiliar with how to create a class, see "Creating Classes with VBScript 5.0," December 1999.) Importing classes is easy with WSH 2.0 because WSH 2.0 has a new file format (.ws) to include files. To import a class with WSH 2.0, isolate the class code in a .vbs file and use WSH's <job> and <script> elements to include and utilize the class.

For example, suppose you want to import the VBScript class FileList.vbs, which manages files that meet a certain filename specification in a folder. Use the New keyword to instantiate the class. This approach allows seamless use of class properties and methods, much like working with COM objects.

With WSH 1.0

If you use WSH 1.0, use the VBScript 5.0 runtime evaluation function ExecuteGlobal. First, isolate the class code in a .vbs file. Then, include a custom subroutine to read and evaluate the file’s content at runtime. Use a line like Include "MyClass.vbs" in your script to import the class.

Porting VB and VBScript Classes

VBScript and Visual Basic for Applications (VBA) share many similarities, enabling straightforward code porting.

Porting VBA Classes to VBScript

Wrap the VBA class code with Class...End Class and adjust for syntax differences (e.g., replacing unsupported keywords like Friend or Static).

Porting VBScript Classes to VB

Copy the VBScript class code into a .cls file in a VB project and resolve syntax discrepancies.

For more information, consult Microsoft's references on VBScript and VBA features.

Recycle Your Code

If you isolate your VBScript classes in separate .vbs files, you can import the classes' functionality wherever you need it—whether that's in a different file or a different application. You can also port your existing VBA classes to VBScript code and vice versa. Whether you import or port classes, the end result is the same. You save time and effort because you can reuse already-proven code.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like