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 me

Other languages often use this or self to access the current object. To do the same thing in VBScript, use the me keyword.

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.

Published February 15, 2007
Categorized as ASP
Short URL: https://snook.ca/s/766

Conversation

14 Comments · RSS feed
Graham said on February 16, 2007

I still do some ASP work and try to avoid VBScript like the plague because of how verbose it is, and horrible things like dictionary objects and crappy arrays. I'm happy with JScript, but I never delved into OOP VBScript so this is a nice pointer :)

Chris McLeod said on February 16, 2007

I develop in VBScript ASP every single day (inc. OOP in the last 6 months); I'm sure it's punishment for crimes in a past life. No matter how many dazzling proposals I put forward, I can't get The Powers That Be to move forward onto more modern languages and platforms.

Jason Kataropoulos said on February 16, 2007

Jonathan, I still do code in ASP. I am moving to C#, slowly though.

Unfortunately, at work, we have around 200 websites developed in ASP and around 400 in dot net and php and we have to provide support for the oldies. We are trying to make our clients upgrade to our new CMS but some are not whiling to do so.

I have been extensively using classes in ASP (VB Script) during the last 3 years, and they do provide some OO thingies. When it comes to truly utilizing OO concepts and getting the most out of them, VB Script lacks big time!

Other than that, I have to admit that I have done loads of nice things with classic ASP and I have been making a living out of it for the last 10 years.

ChadL said on February 16, 2007

While I'm happy to say I managed to avoid any ASP and VBScript for webdev, I wasn't able to avoid in a few MS Access (eww) applications.

Same old VBScript, same old 'me'. Don't miss it one bit.

:)

Gilbert West said on February 16, 2007

Ditto Jason Kataropoulos really, there are a lot of sites out there that still work well and clients tend to incrementally add new features rather than wholesale change to a new site and server-side language that they don't really care about (and why should they).

ASP still enables you to build really good sites and if you're been a good coder and built up a decent code library over the years then you can still quick deploy good sites.

My only regret is that i didn't use Javascript as the server-side language, as that would have made the marriage of the server-side and client-side development a lot easier.

Jason Kataropoulos said on February 16, 2007

I so much agree Gilbert. :)

Andrew Herron said on February 16, 2007

Yup, still using VBScript for ASP at my current job. The new softwares are .NET but for all our legacy clients it's ASP.

Thanks for the great time, I'll certainly put it to use!

Andrew Herron said on February 16, 2007

I so meant thanks for the great tip.

Chri said on February 16, 2007

Like a few others that have commented I made my living out of mainly VBScript and ASP for several years. In fact it's only with joining a new company this January I've left it behind for good and am now using C#, which has good and bad points.

It's amazing how many websites - even fairly recent ones - still use a technology which is so much derided by "proper" developers, and is certainly a few years out of date. For example see http://photocase.com.

It just goes to show you it's not necessarily using the latest languages that makes the difference, it's how well you build your apps (or websites etc).

Lance Fisher said on February 16, 2007

Wow, I never thought that I'd be seeing new blog posts on Classic ASP. Back in the day, I'd make data access components, etc. in VB and use them in ASP as COM objects. They performed better because they were compiled, plus it more like OOP.

I'm am 100 times happier with VB.NET and ASP.NET.

qiuyun said on February 17, 2007

反对撒反对撒 日

Andy Kant said on February 19, 2007

I'm glad that I was using PHP in those days. However, I have had to maintain ASP classic sites as well as port them to ASP.NET which wasn't too much fun. VBScript scares me, or maybe not VBScript specifically, but the people that programmed in it -- poorly.

@Chris
C# doesn't really have many weaknesses aside from that there isn't official Microsoft support for .NET on platforms other than Windows. I'd assume that you are primarily referring to ASP.NET about bad points, not my beloved C#.

I've found that most of the time these days, I've been using C#/ASP.NET as my primary freelance web development platform. Not really because I think its the best (I usually use PHP or occasionally RoR for personal projects), but there's just so many spectacular .NET commercial libraries that save a ton of development time and money. Plus, it doesn't hurt that C# is a joy to develop in (the only language I find to be more fun is Ruby, which is limited in its usefulness).

Bob Laidig said on March 15, 2007

There is 1 caveat to this. The me. syntax only works on Public properties/methods. If a property/method is declared Private, me. won't have visibility to it.

sze said on March 27, 2007

Thank you for this article. I was able to get up and running with the examples you provided:

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

Dim a
set a = new myClass
a.count = 0
a.countTwice
a.countTwice
WScript.Echo a.countTwice

I am curious, can we extend these classes?

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.