Home | Web Design Samples | Art & Design | Multimedia | Services | Contact | E-mail Me
Flash Chat | Flash Test | JavaLab | Pic Resizer | Articles | Snip A Site | Link Lister
| Pay James
Find out what's happening with the Web locally—Check out my Meetup at the Brampton Web Design Meetup Group.
I have worked with companies in the U.S.A, Canada and Europe and can help you with your Web development and design on an hourly or project basis. Contact me for an estimate or give me a call.

A Simple ASP Class Reference Example

The code below shows a simple class called Dog. Just read the comments and follow through. Not much use, but a decent way to show how a class is made using ASP.

<%
'THIS TUTORIAL SHOWS HOW TO CREATE A CLASS IN VBSCRIPT by James Tadeo
'YOU CAN LITERALLY COPY AND PASTE THE CODE AND SAVE IT AS A .ASP FILE

'------------------------------------------------------------------------------

'This class describes dog methods and properties

Class Dog

'--------declare private variables----
'list the variables you will be using
'the "m_" denotes it is a member of this class
'the Private keyword means it can ONLY be used in this class
'if you set the scope to Public, then the variable can
'be seen and changed by code outside the class

Private m_strColor

Private m_strName

Private m_strBreed 'in this example we don't use this

'--------public properties-----------

'--------------COLOR PROPERTY---------------
'This let statement is used to set the value of variable within a class
'ByVal means that the code will be working with
'a "copy" of theColor parameter
'vs ByRef, in which you are working with the "actual"
'parameter and can change the value

Public Property Let Color(ByVal theColor)
m_strColor=theColor
End Property

'this "gets" the color and returns/assigns m_strColor to it
Public Property Get Color()
Color=m_strColor
End Property
'--------------END OF COLOR PROPERTY--------


'--------------DOGNAME PROPERTY----------------
'we can set the dog name like this after creating or instantiating the object:
'MyDog.DogName="Benson" <---this code is used in the below to set the value of DogName
'We are basically saying, "I want you to stuff the name Benson into the variable DogName 'that you can find in the Class Dog, after you've instantiated the Class Dog."
Public Property Let DogName(ByVal theName)
m_strName=theName
End Property

'MyDog.DogName is used to retrieve (no pun intended) the value of DogName
'which in our example is named Benson
Public Property Get DogName()
DogName=m_strName
End Property
'--------------END OF DOGNAME PROPERTY--------


Public Property Let Breed(ByVal theBreed)
m_strBreed=theBreed
End Property

Public Property Get Breed()
Breed=m_strBreed
End Property


'--------public methods--------------
Public Sub Bark(ByVal NumBark)
Response.Write "<br>"
Response.Write "Quiet down that dog! It's barking! <br>"
Response.Write "It has barked " & NumBark * 3 & " times!"
End Sub

'--------Events (NOT USED IN THIS EXAMPLE)-------------
'page 125 of the book has a good explanation of this, in short it says:
'in a VBScript class there can only be 2 scripts that are fired
'They are: Class_Initialize() event and the Class_Terminate() event
'Class_Initialize() event is fired or happens when an object is instantiated from the class
'Class_Terminate() event is fired when the object is destroyed
'initalize event is a good place to initalize any of the private variables in your class
'the terminate event can be used to do cleanup
'in essense, these two events are really subroutines
'You can put them as subs

Private Sub Class_Initialize()
'initialize code goes here
End Sub

Private Sub Class_Terminate()
'cleannup or terminate code goes here
'this will take you to my Web site when the
'Set MyDog=Nothing is called in the code below around line 84
'Response.Redirect "http://www.jamestadeo.com"
End Sub

End Class

%>

<%
'Okay, the moment you've been waiting for, let's make a dog class!

'1. Dimension an object variable and call it dog
Dim MyDog

'2. Create the dog object with the MyDog reference
Set MyDog=New Dog

'3. Now you can work with it, in this example we set the color
MyDog.Color="purple, blue and green"

'we can set the dog name
MyDog.DogName="Benson"

'we can set the dog breed!
MyDog.Breed="Manchester Terrier"


'4. Show the dog color
Dim DogColor, strDogName, strBreed
DogColor=MyDog.Color
strDogName=MyDog.DogName
strBreed=MyDog.Breed

Response.Write "The dog color is: " & DogColor & "<br>"
Response.Write "The dog name is: " & strDogName & "<br>"
Response.Write "The dog breed is: " & strBreed & "<br><br>"


'You can also access it directly like this
Response.Write "You can access the property directly also: <br>"
Response.Write "The dog color is: " & MyDog.Color & "! <br>"

'Here, we call the Bark method and exaggerate this number by 3
MyDog.Bark 5

Set MyDog=Nothing
%>


Comments?

Contact me now with my online e-mail application:

Mailing Address: 12 Paradise Gardens, Brampton, ON L6S 5C7 Phone: (905) 793-2690  Fax: (905) 790-7412
© Copyright 2003 James Tadeo All trademarks, copyrights, and branding belong to their respective owners.