Custom Class Objects in VBScript - Creating Code Classes in VBScript
(Page 2 of 4 )
Before we begin creating actual classes, let’s take a moment to think about when they should be used. Classes should contain groups of related properties, methods, and events.
For instance, we could create a code class that is used to find information about cars in our inventory. The class could return a Car object. This object would most likely have some properties that set or return information about the car, such as Color, Size, Make, or Model. It might also have methods that refer to actions performed on or by the car, such as StartEngine, EngageBrake, or BeginSale. Finally, the Car object could also expose methods that notify our script or program when certain events happen to the car. These might be EngineStarted, ScheduledMaintenance, or SaleCompleted.
As you can see, all of the functions that would be needed by a sales lot could easily be packaged into a single code class. This allows the code to be easily ported from one application to another, and also allows for a single point of update, which makes managing future upgrades more feasible. Aside from that, VBScript is an object-oriented programming language, so it only makes good sense to take advantage of that coding style.
A code class in VBScript is quite simply a group of functions enclosed in a Class structure. The scripting engine handles the rest for you. Today we’re going to be creating a class that can create Compressed Folders natively in WSH. So to begin, we’ll create a class named CompressedFolder.
Class CompressedFolder
End Class
Voilà! We now have a code class. When the scripting engine parses this bit of code, it will create an object reference for our new class. We can connect to that in your script using the following syntax.
Set objZipFolder = New CompressedFolder
Of course, at this point our class doesn’t actually do anything. We need to add the relevant code for it to be useful. It’s important to note that, with the exception of variable declarations, all code in a class must be procedural. In other words, all code inside of your class must be contained in either a Function, Sub, or Property block.
Next: Adding properties to a class >>
More Code Examples Articles
More By Nilpo