Now we have the class built lets see how to use it to instanciate (create an instance of) our Car object from an asp page.
Build a page
Lets create a simple html page with some asp embedded into it.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%Option Explicit%>
<!-- #Include File="classCar.asp" -->
<html>
<head>
<title>Asp Class Example</title>
</head>
<body>
<%
Call RenderCar()
%>
</body>
</html>
<%
Private Sub RenderCar()
'
End Sub
%>
Instanciating an object
The first thing you should notice that we reference the class file by using the "#Include File" method. This "imports" the code from our class file into our page. In the RenderCar method add the following code, then we'll go through it bit by bit.
Dim MyCar 'As Car
Set MyCar = New Car
Response.Write "<p>My car's colour is " & MyCar.ColourOfPaint
Response.Write "<p>My car has " & MyCar.NumberOfWheels & " wheels"
Response.Write "<p>If I now paint my car red..."
MyCar.ColourOfPaint = "Red"
Response.Write "<p>My car's colour is now " & MyCar.ColourOfPaint
With MyCar
.StartEngine()
.SelectGear(6)
.SelectGear(1)
.DepressClutchPedal()
.SelectGear(1)
.ReleaseClutchPedal()
.StopEngine()
.StartEngine()
.DepressClutchPedal()
.StartEngine()
.StopEngine()
End With
If Not(MyCar Is Nothing) Then
Set MyCar = Nothing
End If
Now breaking it down into bite sized chunks... First we declare a new variable "MyCar" to hold an instance of our "Car" object and then set the variable to reference a newly created instance of that object. Note the syntax for setting the reference; unlike setting a value of an integer where we would use intThis = 12, we must use the Set word so Set ThisVariant = ThatObject.
Dim MyCar 'As Car
Set MyCar = New Car
As we used the "Sub New" method in the class to populate some values into the properties as soon as we instanciate the object we can retrieve them right away with... [MyObjectName].[PropertyName] in this case MyCar.ColourOfPaint and MyCar.NumberOfWheels.
Response.Write "<p>My car's colour is " & MyCar.ColourOfPaint
Response.Write "<p>My car has " & MyCar.NumberOfWheels & " wheels"
Now we can change the value of one of our objects properties with [MyObjectName].[PropertyName] = Value. In this case we will change the paint colour to red. MyCar.ColourOfPaint = "Red" and then display the value of the property as before.
Response.Write "<p>If I now paint my car red..."
MyCar.ColourOfPaint = "Red"
Response.Write "<p>My car's colour is now " & MyCar.ColourOfPaint
Next we will call some of the methods of the object. To do this we use [ObjectName].[MethodName]. Also to make the code clearer to read you can use With [ObjectName] and End With blocks. This saves writing out [ObjectName].[MethodName] for each method call. Then you just need to use dot[MethodName] or dot[propertyName]
MyCar.StartEngine()
With MyCar
.SelectGear(6)
.SelectGear(1)
.DepressClutchPedal()
.SelectGear(1)
.ReleaseClutchPedal()
.StopEngine()
.StartEngine()
.DepressClutchPedal()
.StartEngine()
.StopEngine()
End With
I wont go into the details of what each method does as this should be clear from the method name. What is important is how you access the methods from the page. Please note that any class methods that were declared Private will not be available for use on the page.
Last of all we have to dispose of the object properly to release the memory that the object resided in.
If Not(MyCar Is Nothing) Then
Set MyCar = Nothing
End If
We do this by setting the variable that references the object to reference of Nothing (after of course, checking it was set in the first place).
Multiple instances on one page
So what if we need to have multiple instances on one page? Well all we need to do is declare a different variable to reference each instance.
Dim MyCar 'As Car
Dim YourCar 'As Car
Set MyCar = New Car
Set YourCar = New Car
MyCar.ColourOfPaint = "Black"
YourCar.ColourOfPaint = "White"
Response.Write "<p>My car's colour is " & MyCar.ColourOfPaint
Response.Write "<p>Your car's colour is " & YourCar.ColourOfPaint
You can have as many instances on the page as you like, but each must be referenced from a variable. Try it and see. Remember to clear up all your objects at the end by setting all their references to nothing.
I hope this gives many novice programmers who have not yet used classes in classic asp the inspiration to get on and try it. To me when I started using them it seemed like a lot of effort for what seemed very little gain. But there are many advantages to programming this way, code maintenance of my apps is now much less troublesome, with it being a lot easier to add additional functionality. The code in the pages are now easier to read through as there is little "business logic" embedded within in them, and with all my classes in one folder its easy to find the relevant class and copy it to a new aplication, so developing new applications is getting much quicker. Lastly, but not least is that the transition to ASP.Net if you choose to do it later, will be a little smoother.
Hopefully in the not to distant fututre I will publish an article on using objects wrapped up inside a dictionary object as an alternative to bringing a recordset back to your page or to using GetRows() and arrays. It will also use a dataAccess class to abstract the dataAccess out of the business logic, and get a tiny bit closer to the 'N' tier programming approach.
A point to note
Although in this case I have used Response.Write within the class to output a result, in practice this is not a good idea. One of the advantages of using classes is to abstract out the "Business Logic" of the page from the "Presentation Code" (html) so it is much better practice to populate a property with the results of your method calls and retrieve the value from the property as shown with Response.Write MyCar.ColourOfPaint in the above example.
Thanks for reading this, I hope some of you find it useful. Your comments, good or bad, are very welcome as this is my first article.
Comments