To read the article online, visit http://www.4GuysFromRolla.com/webtech/092399-1.3.shtml

Using Classes within VBScript, Part 3

By Mark Lidstone


Let's take another quick look at the code snippet from Part 2.

Class TVProgram Public StartTime Public internal_ProgramDate Public Property Get ProgramDate ProgramDate = Day(internal_ProgramDate) & _ " " & MonthName(Month(internal_ProgramDate)) & _ " " & Year(internal_ProgramDate) End Property Public ProgramTitle End Class Dim objTVShow Set objTVShow = New TVProgram objTVShow.StartTime = CDate("17:30") objTVShow.internal_ProgramDate = DateSerial(1999,9,17) objTVShow.ProgramTitle = "The Jerry Springer Show" Response.Write objTVShow.ProgramTitle & " is on at " & _ objTVShow.StartTime & " on " & objTVShow.ProgramDate & "."

When you ask for the ProgramDate property of the object now, it actually executes the function called ProgramDate. This function is defined in a special way as shown above, with the usual (you will get used to them) Public|Private keyword at the start of the declaration. The Property keyword tells the interpreter to use the function externally as if it were a normal property. The next word, Get tells the interpreter whether this function should give out or take in a value.

Get means "Allow the external code to *get* a value". The two other keywords to go in place of Get are Let and Set, but they are a little more complicated so we'll move onto those later.

The way you return the value of the function is exactly the same as the way you would return a value from any other function, so essentially what we've done here is create a function embedded in an object.

Now the code looks a little ugly because we have to set the value with objectname.internal_ProgramDate and then call it with objectname.ProgramDate. Wouldn't it be better if we could both set the value and retrieve it with the same keyword? Well, that also is possible.

By giving a Property Get and Property Let statement the same names, we can pretend they are the same property of the object *but* only if the Get and Let statements both have the same number of arguments (it may look different below, but read on for an explanation).

Class TVProgram Public StartTime Public internal_ProgramDate Public Property Get ProgramDate ProgramDate = Day(internal_ProgramDate) & " " _ & MonthName(Month(internal_ProgramDate)) & _ " " & Year(internal_ProgramDate) End Property Public Property Let ProgramDate(ByVal varDateIn) internal_ProgramDate = CDate(varDateIn) End Property Public ProgramTitle End Class Dim objTVShow Set objTVShow = New TVProgram objTVShow.StartTime = CDate("17:30") objTVShow.ProgramDate = "17 Sept 99" objTVShow.ProgramTitle = "The Jerry Springer Show" Response.Write objTVShow.ProgramTitle & " is on at " & _ objTVShow.StartTime & " on " & objTVShow.ProgramDate & "."

Now, you can see from the code above that the Let statement seems to have an extra argument. This confused me for ages while I was trying this for the first time. Every time I used zero parameters on both property declarations I would get an error to the tune of Number of arguments must be equal. "They *are* equal!" I kept screaming back. I felt more than a little stupid when I went back and re-read the docs! :)

The reason for this is that when you try to set a value to ProgramDate you will be using a line like:

objTVShow.ProgramDate = dtmMyDate

So for ease of use, the value on the right of the equals sign (dtmMyDate in this case) is passed to the function as an argument. This means that the interpreter was probably thinking something along the lines of "Get ProgramDate has zero arguments so Let ProgramDate needs one more.....uh oh!". The assigned value is always passed as the last argument to the property, so even if you use other arguments you will always be able to get the assigned value as the last argument.

Now, back to the program. It's all very well and good that it's possible to set the date as either text with ProgramDate or as a date variant with internal_ProgramDate but wouldn't it be easier if you could use just one "point of contact" so to speak? Well, again you can!

If we tell the interpreter that internal_ProgramDate should only be available internally, we could get Let ProgramDate to check what type of data it is being passed and use it accordingly. e.g.

Class TVProgram Public StartTime Private internal_ProgramDate Public Property Get ProgramDate ProgramDate = Day(internal_ProgramDate) & " " & _ MonthName(Month(internal_ProgramDate)) & _ " " & Year(internal_ProgramDate) End Property Public Property Let ProgramDate(ByVal varDateIn) If IsDate(varDateIn) Then internal_ProgramDate = varDateIn Else 'Place some error handling code in here. End If End Property Public ProgramTitle End Class ...

And giving the same kind of treatment to the StartTime property as well gives:

Class TVProgram Private internal_StartTime Public Property Get StartTime StartTime = Hour(internal_StartTime) & ":" _ & Minute(internal_StartTime) End Property Public Property Let StartTime(ByVal varTimeIn) If IsDate(varTimeIn) Then internal_StartTime = varTimeIn End If End Property Private internal_ProgramDate Public Property Get ProgramDate ProgramDate = Day(internal_ProgramDate) & " " _ & MonthName(Month(internal_ProgramDate)) & _ " " & Year(internal_ProgramDate) End Property Public Property Let ProgramDate(ByVal varDateIn) If IsDate(varDateIn) Then internal_ProgramDate = varDateIn End If End Property Public ProgramTitle End Class ...

Now this code is starting to get a bit unwieldy for what we want, and we want to be able to use the TVProgram class in other pages, so the easiest thing to do would be to put the TVProgram class definition in it's own file and include it in all the pages where it is needed. We'll examine this in Part 4!

  • Read Part 4! (Part 1 | Part 2)


  • Article Information
    Article Title: 4GuysFromRolla.com - Using Classes within VBScript
    Article Author: Mark Lidstone
    Published Date: Thursday, September 23, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/092399-1.3.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers