Using Classes within VBScript, Part 2By Mark Lidstone
The first thing you need to know about creating you own object types (classes) in VBScript is that there is no "voodoo magic" or really difficult parts, so your pet chicken is perfectly safe :). It is, actually, amazingly easy! I taught myself the basics in an afternoon just from reading the Microsoft VB Script reference, but I have to admit that they are not the easiest documents to read.
For starters, you'll need version 5.0 of the VBScript engine. This can be downloaded at Microsoft's Scripting Site.
Let's get onto the code. Classes are defined in a very similar way to functions and subs. They start with the line
Class <MyClassName> and end with the line
End Class. All the object definitions go inside these lines. Using just what we know now we can build our very first class. One that does nothing:
This may not look like much, but when you write those lines in your code you will be able to create an instance of your object with the following code:
We obviously can't do anything with the object yet so now I'll explain how to define properties and methods in the object.
The most basic thing you could do with an object is hold a set of data. For instance, if you want to hold the time, date and title of a television program altogether in memory you could create an object that has the properties "StartTime", "ProgramDate" and "ProgramTitle". The code for this is shown below:
The way this code works, is we define
ProgramTitle as properties inside the
TVProgram class. In this case the properties are treated exactly like other variables and no code is executed when the value is set or taken. The
Public keyword before the property name holds real meaning here and can be very important. If you don't explicitly define a method or property as public or private it will default to public, but it is good coding practice to define everything (for one thing, it will be easier to read when you come back to look at it later).
The output from the code above should look something like (depending on your server's locale settings):
(I'm from the UK, so the date should look like that). This may work fine for whatever project you are working on, but the real power of objects comes when you start to use the functionality of other objects to create one seamless interface to all the information and functionality you could need to do with the entity you have based your object around.
Now, let's say that you aren't happy with the way that the date is being displayed in the above example and you want to always display the date in the same format. There's no need to add
FormatDateTime() around each call to the ProgramDate property, you can embed that kind of code inside the property itself.
The way to do this is define the property in another way. Again, we'll use the name
ProgramDate for the externally visible property, but because the property called
ProgramDate is now going to be a function instead of a static value we'll store the actual date in a property with another name. Let's call it
The output from the code above should now be more like: