Self-referencing in VBScript
Does anybody actually develop in VBScript anymore? If you did ASP development, you likely did so in VBScript. ASP was the first server-side language I got into heavily. It took me learning Java and all the object-oriented programming concepts that go with it before I looked to see if the same thing could be accomplished with VBScript.
While the OOP features in VBScript are pretty minimal, you can still pull off some class constructs. Part of that is having the flexibility of easily referencing the current object properties from within an object.
The basic class
Class myClass Private i_count Public Property Get count count = i_count end Property Public Property Let count(c) i_count = c end Property End Class
In this example, a private variable is declared and can be referred to internally. However, there might be times where any access to a variable requires certain code to execute. Having a central point of access to do this, even from within the object, would be nice.
It's all about
Other languages often use
self to access the current object. To do the same thing in VBScript, use the
Class myClass Private i_count Public Property Get count i_count = i_count + 1 count = i_count end Property Public Property Get countTwice countTwice = me.count + me.count end Property Public Property Let count(c) i_count = c end Property End Class
In this code example, each access of the count property requires it to be incremented by one. Using the
me keyword we can leave the getter to do the grunt work.