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

7 Min Read
ITPro Today logo in a gray background | ITPro Today


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 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," http://msdn.microsoft.com/workshop/languages/clinic/ scripting091399.asp.)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 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. You can download WSH 2.0 at http://www.microsoft.com/msdownload/ vbscript/scripting.asp.To import a class with WSH 2.0, you need to isolate the class code in a separate .vbs file and use the code in Listing 1. This code, which goes into the .ws file you use to import the class, contains WSH 2.0's new and

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