Thursday, February 3, 2011

How to Creat Class in VB Script

Creating classes in VBScript is really no different than creating classes in any other programming language. The concept of an object is usually described as just about anything. When you think about it every thing around you is some sort of object. For example a house is an object. It has properties and methods. The properties could be that the house has 3 bedrooms, a garage, a pool, air conditioning etc. You can even break down the house into smaller objects, such as doors windows etc. A garage door may have methods that are controlled by a remote. The methods may be something like garageDoor.open or garageDoor.close. The garage door would be an object, and the method would be to open or close. Getting back to the Vb Script, lets see how we would write a simple class called Math that adds or subtracts two numbers. First let’s look at our parameters for the class.





Class Name: Math

Methods: add and subtract





Now before we actually create the class, lets just see how we would write the two methods:





Public Function add(afirst, asecond)

Dim output

output = afirst + asecond

add = output

End Function





Public Function subtract(sfirst, ssecond)

Dim output

output = sfirst - ssecond

subtract = output

End Function





Now we can define the class:





Class Math


End Class





Then just put them together:





Class Math


Public Function add(afirst, asecond)

Dim output

output = afirst + asecond

add = output

End Function





Public Function subtract(sfirst, ssecond)

Dim output

output = sfirst - ssecond

subtract = output

End Function


End Class

No comments:

Post a Comment