VBScript Classes pt2 – Properties
This week I thought I’d continue the discussion on classes, from my previous article on the subject (VBScript Classes pt1). One of the nice things about classes is the ability to call and manipulate them like variables, while having a lot of code in line to do things along the way.
Last week you saw we can embed functions (and subs) within classes to give encapsulation, but you can also embed some new method types “properties”.
An example of a typical method, is the good old wscript object. You’re probably very familiar with wscript for example:
wscript.echo "Hello World"
Here we call the wscript object, ‘echo’ method and send it “Hello World” a parameter. This is akin to calling a sub within VBS.
How about if we want to do something akin to setting or retrieving the title of an explorer window?
objIE.document.title = "Hello World" wscript.echo objIE.document.title
Though it might not be obvious – something very interesting is happening here – you can both set the title, and get the title using the same call, depending on the situation.
this is what class ‘properties’ give you. We can set both “get” and “let” properties with the same name to handle the situation where we are retrieving, or storing a value. Of course we don’t have to set both, you can have read-only and write-only properties if you like.
I’ve created a simple example below for you – it’s an address book program using two classes. One called clsIDCard which is a simplistic name and phone number handler, and one called clsAddressBook which maintains a list of cards, lets you get and add them, and retrieve how many cards there are.
Now, you could use a few arrays to do this, but using classes lets us add some clever functionality.
for example, when we add a card in this example, we get to set a value for objTempCard.fullname (in the add sub of clsAddressBook). This in turn goes through the property add code in clsIDCard, which you can see extracts the first and surname into the card object.
Thus, even though we’re just setting some values with our call, the data’s being manipulated on the fly for us.
The addressbook itself ends up as an array of idCard objects, so all the code in clsIDCard can be used for each of them – if we wanted to add validation, new fields of information etc, we can do that in isolation of any other piece of code (though we might want to expand the ‘add’ function to make it easier to handle them). Also if we want to change the layout of the ‘card’ property, we can do that without changing the addressbook logic.
I hope this helps you get ‘classes’ into your head. Feel free to comment if not.
Option Explicit Class clsidCard Private strSur, strFirst,strMobile Property Let surname(str) strSur = str End Property Property Get surname surname=strSur End Property Property let firstname(str) strFirst = str End Property Property Get firstname firstname=strfirst End Property Property Let fullname(str) strfirst = trim(left(str, InStr(str," "))) strsur = Trim(Mid(str, InStr(str," "))) End Property Property Get fullname fullname= strfirst & " " & strSur End Property Property Let mobile(str) strMobile=str End Property Property Get mobile mobile=strmobile End Property Property Get card card="Name: " & fullname & vbNewLine & "Mobile: " & mobile End Property End Class Class clsAddressBook Public addressbook() Sub class_initialize ReDim addressbook(0) End Sub Sub add(ByVal strName,ByVal strMobile) Dim objTempCard If IsObject(addressbook(UBound(addressbook))) Then ReDim Preserve addressbook(UBound(addressbook)+1) End If Set objTempCard = New clsidCard objtempcard.fullname = strName objTempCard.mobile= strMobile set addressbook(UBound(addressbook)) = objtempcard End Sub Function getcard(index) set getcard = addressbook(index) End Function Property Get count count= UBound(addressbook)+1 End Property End Class Dim idCard : Set idcard = New clsidCard Dim addressbook : Set addressbook = New clsAddressBook addressbook.add "Simon Hunt","239 298 7000" addressbook.add "John Smith","239 298 7001" addressbook.add "Alice Bob","239 298 7003" Dim i For i = 0 To addressbook.count-1 Set idCard = addressbook.getcard(i) WScript.Echo "Card: " & i & vbNewLine & idCard.card & vbNewLine Next
Great posts, Simon, now where is the class for sbadmcl 🙂 ?