This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Encapsulation and classes are not new concepts, but developers using VBScript have long gone without. VBScript 5.0 brings classes to the masses. |
Hello, Dave
Here is a simple example that shows the declaration of a class and the creation of an instance:
|
The class Greeter is declared, then an instance of that class (MyGreeter) is created as an object. The properties and methods of that object can then be set and retrieved with the dot operatorthe same way that you'd access members of any other object in VBScript.
Unlike Visual Basic, VBScript uses the Class…End Class statements to demarcate the contents of the class. Visual Basic uses a separate .cls file to define a class, but since VBScript might be included in a Web page where there is no file system per se, this syntax allows classes to be declared in-place. (Because the VBScript syntax is supposed to be a subset of Visual Basic, the Visual Basic team expects to add this syntax to a future version.) Note that the property (Store) and the procedure (SayHello) are declared as Public. Data members and procedure members may also be declared as Private. Private members may only be accessed from within the class, not by any callers of a class instance. Private members allow the class implementer to encapsulate class functionality into procedures and properties that need not be accessed directly by the caller of the class. VBScript 5.0 is not strictly an object-oriented language like C++ or Java. There is no notion of polymorphism or inheritance in VBScript 5.0. Though VBScript classes allow you to define your own objects, you cannot define an object hierarchy where, say, a Terrier class is a subclass of Dog, which is a subclass of Mammal. VBScript classes are merely a way to group data and the operations on the data together to improve encapsulation.
A Simple Abstract Data Type
Pick a Card, Any Card
A Few Technical Points
At the end of this little program, neither X nor Y have been released because each still has another object referencing it. Yet the programmer has no way of getting to that memory because all the variable references have been thrown away. This is a circular reference memory leak.
Unlike Microsoft® JScript®, VBScript does not have a circular reference detector. Fortunately, that memory will eventually get released. When the hosting application shuts down the script engine, the VBScript engine breaks all the circular references and destroys all unterminated objects. Still, you should avoid circular references because they do waste resources. A somewhat more technical problem can arise when using VBScript classes on the ASP Web server. The ASP server is multithreaded and assigns a different thread to each page request (and hence each script engine). But VBScript class instances are apartment-threaded objects, which means that they must run on the thread that created them. Therefore, attempts to use one instance of a VBScript class on two different pages via storage in Session or Application scope are doubly doomed to failure. First of all, since apartment-threaded objects must run on the same thread, but different pages are on different threads, you'll take a major performance hit if you use one object on two pages with Session scope. Any calls on the second thread have to be marshaled back to the first thread, which is both a source of resource contention and simply a slow operation. Putting apartment-threaded objects into Session scope is not recommended, and putting them into Application scope is illegal. However, that point is somewhat moot because, as I mentioned earlier, when the script engine shuts down, all of the objects that it owns are terminated. Remember, the methods of a class are script and therefore need a script engine to run them. When the page associated with the class is served to the client, the script engine that created the class is closed and all the class instances go with it. Fortunately, it's not all bad news: Windows Scripting Components allow you to encapsulate script functionality into objects as well, and WSC objects have lifetimes independent of the page that created them. Unfortunately, they do still suffer from the threading limitations. Space does not permit a full explanation of WSCs here, so watch for further articles on the Windows Script Component technology.
Conclusion
|
From the November 1999 issue of Microsoft Internet Developer.
|