|
||||||
Starting Object Oriented Programming in VBScriptHow to Create, Use and Reuse OOP Classes in a VBScript ApplicationFor 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:
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 ClassA 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:
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 ClassesWith 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 ApplicationThe 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.
|
||||||
|
|
||||||
|
|
||||||