When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Thursday, September 23, 1999

Using Classes within VBScript

By Mark Lidstone

Before I launch into the actual article and explain how to build your own objects, I want to make sure that you know the terms associated with objects. It is possible to be use objects to program without knowing the correct terms, but it isn't recommended! For those who are new to objects, the next section will introduce you to object concepts and vocabulary. Those of you who already know the basics of Object Oriented Programming (OOP) can skip this next bit...

- continued -

First things first:
  • "What's an object?" - An object is basically a container for a set of variables and functions, and an object is normally used to represent some kind of entity.
  • "What's an entity?" - An entity is literally a "thing". What I mean by "thing" is an idea or object of any kind. For instance, you could say a car is an entity because it is an object. You could also say that a sale of a product by your sales department is an entity, and you could then break that down and say that the salesperson that makes the sale is an entity and the customer is an entity, and the product is an entity.....

    Let's take the "sale" entity (object) and explore the idea further. To actually have an "image" of the sale in your object you would need to know what the customer bought, which customer was involved, who the salesperson was etc..... This may seem like simple stuff, but what if all that information was stored in separate database tables, so to get all the information about a given sale you would need to run separate queries on your database to pull all the data together. Wouldn't it be easier to hold all the information about the sale in one place?....can you say "object"? :)

    Inside your object you can embed the code to get information from other tables, and you could store all of this information in properties of the object to make playing with sales in your code much easier. E.g. instead of doing something like this:

    'Open the database connection Set objConn = Server.CreateObject("ADODB.Connection") objConn.Open "MyDSN" 'Create the recordset object Set objRS = Server.CreateObject("ADODB.Recordset") 'Define the SQL query strComplexSQLQuery = "SELECT C.Name, S.Name FROM Customers C, " & _ "Salespeople S, Sales Sl WHERE Sl.CustomerID=C.ID AND " & _ "Sl.SalespersonID=S.ID AND Sl.ID=" & strIDOfThisSale & ";" 'Open the recordset objRS.Open strComplexSQLQuery, objConn, adOpenForwardOnly, _ adLockReadOnly, adCmdText 'Take the customer and sales person names from the recordset strCustomerName = objRS(0) strSalesPersonName = objRS(1) 'Tidy up the objects objRS.Close objConn.Close Set objRS = Nothing Set objConn = Nothing 'Output the data Response.Write "This sale was made by " & strSalesPersonName & _ " to " & strCustomerName

    you could use objects to do something like this:

    'Create the "Sale" object Set objSale = New Sale 'Lookup the correct sale objSale.ID = strIDOfThisSale 'Output the data Response.Write "This sale was made by " & objSale.SalesPersonName & _ " to " & objSale.CustomerName 'Tidy up the objects objSale.Close Set objSale = Nothing

    and if you wanted to use the "Sale" object for more than printing a sentence, then the code can save you even more typing.

    In computing terms, objects contain two things. These are "properties" and "methods". A property is basically a variable that is stored inside the object and is used in exactly the same way. The only difference being that instead of using strMyVar = "This is a string variant", you would use objObject.Property="This is a string variant". This is pretty simple to understand but can be very useful. Methods can be thought of as functions and procedures that are embedded in the object, so instead of doing something like strMyVar = FunctionName(strMyVar), you would do strMyVar = objObject.MethodName(strMyVar). The terms may be new, but the functionality shouldn't be. An example of a property is ExpiresAbsolute in the Response object as used in Response.ExpiresAbsolute = CDate("1 September 1999"). An example of a method could be the Write method in the Response object, as in Response.Write "Hello world!"

    The ability to create new types of objects yourself without resorting to expensive and time-hungry compilers is a new feature for VBScript. I'm going to attempt to show you how to create your own object classes and hopefully show you the beginnings of what objects can offer!

  • Read Part 2!

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