ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
XML for ASP.NET Developers
XML for ASP.NET Developers

Find Prices
Read Review
Read Review
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software

Using Classes in ASP

Written on: Nov, 17th 2001.

Working with "ASP classes" is really cool and I am sure, if you read this article, you will also start to write an application in ASP using classes. (Unless you go for ASP .NET) I am very grateful to Dave Cline, who taught me how to work with Classes in ASP.

In order to know more about this article, I assume the reader has knowledge in Object Oriented Programming Structure (OOPS)

How can we declare classes in ASP?

Classes in ASP can be declared as follows

<%
1Class YourClassName
 
2Private intNum
3Private strName
4Public strLabel
 
5Private Sub Method1()
  ' All statments for Method1 goes here
6End Sub
 
7Public Sub Method2()
  ' All statments for Method2 goes here
8End Sub
 
9Public Function Function1()
 ' All statments for Function1 goes here
10End Function
 
11End Class
%>

Now. we will go through each line of the above declaration. Line 1 declares a class called YourClassName Line 2 to Line 4 deals with declaring properties on this class. You can declare either a property to be Private or Public. As VBScript only deals with Variants, we cannot specify the dataType of each property.

Their should be a way to communicate with these private and public properties. So, we need to have methods which talks with these properties. To know more about, Object Oriented Programming in C++, click here

Note: Unless declared Private, all properties and methods are public.

Now, we will see an sample ASP page and a samle class file, which deals with Following is the

Classes.clas - Extension can by anything. I usually used to use .clas
<%
Class test
 
private var1
private var2
private var3
 
Public Sub assign_values(byval arg1, byval arg2, byval arg3)
var1 = arg1
var2 = arg2
var3 = arg3
End Sub
 
Public Sub return_values(ByRef arg1, ByRef arg2, ByRef arg3)
arg1 = var1
arg2 = var2
arg3 = var3
End Sub
 
Private Sub Class_Initialize()
' Initialization statements goes here (You can call this as a CONSTRUCTOR)
End Sub
 
Private Sub Class_Terminate()
' Clearing all the objects goes here (You can call this as a DESTRUCTOR)
End Sub
 
End Class
%>

In the above class file, we have three private properties and two public methods which communicate with the properties. In the following ASP page, we will see, how to invoke the above class.

test.asp
<!--#include file="Classes.clas"-->
<%
 
Dim ObjClass
Dim v1, v2, v3
 
Initialize()
 
Assign 1, 2, 3
 
GetValues()
 
Response.Write v1 & " " & v2 & " " & v3
 
Terminate()
 
Sub Initialize ()
Set ObjClass = New test
End Sub
 
Sub Assign(a, b, c)
ObjClass.assign_values a, b, c
End Sub
 
Sub GetValues()
ObjClass.return_values v1, v2, v3
End Sub
 
Sub Terminate ()
Set ObjClass = Nothing
End Sub
%>

In the above ASP page, we are including the Classes.clas file as the first statement. Then in the procedure called Initialize, we create an instance of class, test. Then, we are passing assigning values to the three properties using the interface (method) assign_values. After that, using the interface GetValues, we are retrieving the values of those three properties. The above is just an example that depicts the usage of classes in ASP. Although, the example just assigns some values to the properties and retrieves those values with the help of another method. We can do a lots of things with classes. The following example

Now, we are going to see a real time example for the use of classes. We are going to create a class called Student, which can hold details of students. We have four properties and two methods. First lets look into the Class definition

Student.clas
<%
Class Student
 
private S_Name
private S_DOB
private S_Sex
private S_Class
 
Sub loadFromHTMLForm()
S_Name = Cstr(Trim(Request.Form("Name")))
S_Dob = CDate(Trim(Request.Form("Dob")))
S_Sex = Cstr(Trim(Request.Form("sex")))
S_Class = Cstr(Trim(Request.Form("Class")))
End Sub
 
Function Save()
Dim StrQuery
' Your connection statments goes here
StrQuery = "Insert Into Student (Roll, Name, DOB, Sex, Class) Values "
StrQuery = StrQuery + "('" & S_Name & "','" & S_DOB & "','" & S_Sex & "','" & S_Class & "')"
connection.Execute strquery
' other statements goes here
Save = "Any Return Value"
End Function
 
End Class
%>

Now, we will see the ASP page, which uses this class.

Student.asp
<!--#include file="Student.clas"-->
<%
Dim ObjStudent
 
Initialize()
 
ObjStudent.loadFromHTMLForm()
 
Response.Write ObjStudent.Save()
 
Terminate()
 
Sub Initialize ()
Set ObjStudent = New Student
End Sub
 
Sub Terminate ()
Set ObjStudent = Nothing
End Sub
%>

Summary

This is just a simple example. You can also use the dictionary object as one of the property in the Class Definition and you can and fill up the dictionary objects and you can use them, while displaying a recordset. As we are using OOP concept, all the advantages of OOP applies in this example too. Happy Programming.

External Links

http://www.gnacademy.org/text/cc/Tutorial/tutorial.html
http://java.sun.com/docs/books/tutorial/java/concepts/

Send your comments to das@aspalliance.com        Back to Article list

 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 11/22/2008 2:33:37 AM