Using Classes within VBScript
By Mark Lidstone
Preface
Before I launch into the actual article and explain how to build your own objects, I want to make sure that you know the terms associated with objects. It is possible to be use objects to program without knowing the correct terms, but it isn't recommended! For those who are new to objects, the next section will introduce you to object concepts and vocabulary. Those of you who already know the basics of Object Oriented Programming (OOP) can skip this next bit...
Let's take the "sale" entity (object) and explore the idea further. To actually have an "image" of the sale in your object you would need to know what the customer bought, which customer was involved, who the salesperson was etc..... This may seem like simple stuff, but what if all that information was stored in separate database tables, so to get all the information about a given sale you would need to run separate queries on your database to pull all the data together. Wouldn't it be easier to hold all the information about the sale in one place?....can you say "object"? :)
Inside your object you can embed the code to get information from other tables, and you could store all of this information in properties of the object to make playing with sales in your code much easier. E.g. instead of doing something like this:
|
you could use objects to do something like this:
|
and if you wanted to use the "Sale" object for more than printing a sentence, then the code can save you even more typing.
In computing terms, objects contain two things. These are "properties" and "methods". A property is basically a variable that is stored inside the object and is used in exactly the same way. The only difference being that instead of using strMyVar = "This is a string variant"
, you would use objObject.Property="This is a string variant"
. This is pretty simple to understand but can be very useful. Methods can be thought of as functions and procedures that are embedded in the object, so instead of doing something like strMyVar = FunctionName(strMyVar)
, you would do strMyVar = objObject.MethodName(strMyVar)
. The terms may be new, but the functionality shouldn't be. An example of a property is ExpiresAbsolute
in the Response
object as used in Response.ExpiresAbsolute = CDate("1 September 1999")
. An example of a method could be the Write
method in the Response
object, as in Response.Write "Hello world!"
The ability to create new types of objects yourself without resorting to expensive and time-hungry compilers is a new feature for VBScript. I'm going to attempt to show you how to create your own object classes and hopefully show you the beginnings of what objects can offer!