Starting Object Oriented Programming in VBScript

How to Create, Use and Reuse OOP Classes in a VBScript Application

© Mark Alexander Bain

Jul 26, 2009
Starting Object Oriented Programming in VBScript, Mark Alexander Bain
For the VBScript programmer object oriented programming is particularly easy. With just a few minutes programming they can develop classes that they can use and reuse

Object oriented programming lies at the heart of many projects, and there is no reason that the VBScript application developer should not benefit from the advantages of using this technique. Advantages such as:

  • the reuse of code
  • the encapsulation of complex functionality
  • the ability to change and add functionality without breaking the applications using the classes

In fact VBScript is a programming language that lends itself very nicely to object oriented programming and, in just a few minutes, the programmer can produce classes with properties that they can then go on and use in all of their applications.

Creating a Simple VBScript Class

A VBScript class is defined by using (rather obviously) the Class...End Class key words, for example:

Class Person
End Class

This creates a empty class, but is not of much used yet. Therefore the properties can be added to it that define exactly what the class is meant to model:

Class Person
public age
public gender
End Class

It's worth noting that elements of the class can be either:

  • public - available to all users of the class
  • private - only available within the class itself

It's also worth storing the class in its own file at this point so that it can be reused again and again in different projects. So, for example the code above could be stored in the file "person.vbs" and it's then ready for use.

Using VBScript Classes

With the class placed in its own .vbs file it can then be used in a VBScript application. The easiest way to do this is to create a windows scripting file (for example "people.wsf") and then to load the class file:

<job id="ShowPeople">
<script language="VBScript" src="person.vbs"/>

The class can then be turned into an object by using the "new" method:

<script language="VBScript">
Set Fred = New Person
Set Mary = New Person

Here two person objects (Fred and Mary) have been created, and so their properties can be set:

With Fred
.age = 21
.gender = "male"
End With
With Mary
.age = 22
.gender = "female"
End With

And then these properties can be used elsewhere in the application:

wscript.echo "Fred is a " & Fred.age & " year old " & Fred.gender
wscript.echo "Mary is a " & Mary.age & " year old " & Mary.gender
</script>
</job>

All that's left to do is to is to run the VBScript code.

Running a VBScript Object Oriented Programming Application

The programmer runs the VBScript application by using Cscript:

cscript /nologo //job:ShowPeople people.wsf

The end result can be seen in figure 1 and it shows the way in which the class is created and used by the VBScript code stored within the Windows Scripting file and the programmer can, of course, use this class in any future applications that they write.


The copyright of the article Starting Object Oriented Programming in VBScript in Windows Programming is owned by Mark Alexander Bain. Permission to republish Starting Object Oriented Programming in VBScript in print or online must be granted by the author in writing.


Starting Object Oriented Programming in VBScript, Mark Alexander Bain
Figure 1: A Simple VBScript Class, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo