Custom Class Objects in VBScript
(Page 1 of 4 )
A code class in VBScript (and most other programming languages) is a structure that houses a set of related properties, methods, and events that can be reused throughout your code. In object-oriented languages, this code class is instantiated as an object reference. In today’s article, I’m going to show you how you can create your own custom VBScript object classes for use in WSH, ASP, or HTML applications.
In VBScript we’re used to using several different objects. The WScript object, the Server object, and the FileSystemObject are just a few that you probably use every day. These objects actually point to code classes. In this case, we’re referring to classes that are housed inside of a dll file somewhere on the system and made available through the Component Object Model (COM).
The terms class, object, and component are sometimes, albeit incorrectly, used interchangeably. An object is a reference to either a class or a component. A class is a local structure that only exists at the time of execution. It is only available to those scripts and programs that can access it directly. It needs to be present in the current script.
A component, on the other hand, is a code package. This package is stored globally on the system and can be reused by any script or program with permission to access it. These components may contain one or more code classes that become available to the local script when the component is instantiated. Most often, these components are pre-registered with the Component Object Model so that they can be referenced globally via their ProgID. In this way, the system itself manages the connection to the component and the programmer does not need to know where the component file is located on the system.
In VBScript, components are instantiated using the CreateObject or GetObject methods. Classes, on the other hand, are instantiated using the New keyword. In each of these cases, an object reference is returned that can be assigned to a variable by using the Set keyword. You should be used to seeing commands like the following.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objRe = New RegExp
The first is a typical use of the CreateObject method to connect to a COM component. The latter demonstrates the use of the New keyword for instantiating a local class named RegExp. RegExp is one of the few local classes provided by the scripting engine itself.
To date you have probably only used the default classes and components that ship with the Windows Scripting Host or with COM, but there are a plethora of others out there that you can install and use. For now though, my next few articles will concentrate on creating your own classes and components.
Next: Creating Code Classes in VBScript >>
More Code Examples Articles
More By Nilpo