When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.
Published: Thursday, September 23, 1999

Using Classes within VBScript, Part 2

By Mark Lidstone


Creating Objects
The first thing you need to know about creating you own object types (classes) in VBScript is that there is no "voodoo magic" or really difficult parts, so your pet chicken is perfectly safe :). It is, actually, amazingly easy! I taught myself the basics in an afternoon just from reading the Microsoft VB Script reference, but I have to admit that they are not the easiest documents to read.

- continued -

'

For starters, you'll need version 5.0 of the VBScript engine. This can be downloaded at Microsoft's Scripting Site.

Let's get onto the code. Classes are defined in a very similar way to functions and subs. They start with the line Class <MyClassName> and end with the line End Class. All the object definitions go inside these lines. Using just what we know now we can build our very first class. One that does nothing:

Class 4GuysTestObject End Class

This may not look like much, but when you write those lines in your code you will be able to create an instance of your object with the following code:

Dim objTestObject Set objTestObject = New 4GuysTestObject Set objTestObject = Nothing

We obviously can't do anything with the object yet so now I'll explain how to define properties and methods in the object.

The most basic thing you could do with an object is hold a set of data. For instance, if you want to hold the time, date and title of a television program altogether in memory you could create an object that has the properties "StartTime", "ProgramDate" and "ProgramTitle". The code for this is shown below:

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

The way this code works, is we define StartTime, ProgramDate and ProgramTitle as properties inside the TVProgram class. In this case the properties are treated exactly like other variables and no code is executed when the value is set or taken. The Public keyword before the property name holds real meaning here and can be very important. If you don't explicitly define a method or property as public or private it will default to public, but it is good coding practice to define everything (for one thing, it will be easier to read when you come back to look at it later).

The output from the code above should look something like (depending on your server's locale settings):

The Jerry Springer Show is on at 5:30pm on 17/09/99.

(I'm from the UK, so the date should look like that). This may work fine for whatever project you are working on, but the real power of objects comes when you start to use the functionality of other objects to create one seamless interface to all the information and functionality you could need to do with the entity you have based your object around.

Now, let's say that you aren't happy with the way that the date is being displayed in the above example and you want to always display the date in the same format. There's no need to add FormatDateTime() around each call to the ProgramDate property, you can embed that kind of code inside the property itself.

The way to do this is define the property in another way. Again, we'll use the name ProgramDate for the externally visible property, but because the property called ProgramDate is now going to be a function instead of a static value we'll store the actual date in a property with another name. Let's call it internal_ProgramDate.

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 & "."

The output from the code above should now be more like:

The Jerry Springer Show is on at 5:30pm on 17 September 1999.

  • Read Part 3! ( or Read Part 1)

  • Software Developer / Programmer - Distributed Systems (NYC)
    Next Step Systems
    US-NY-New York

    Justtechjobs.com Post A Job | Post A Resume


    ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article