Using Classes within VBScript, Part 3By Mark Lidstone
Let's take another quick look at the code snippet from Part 2.
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
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
Let statements both have the same number of arguments (it may look different below, but read on for an explanation).
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:
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.
And giving the same kind of treatment to the StartTime property as well gives:
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!