Home wilkeo>JavaScript    
  How and when to create ASP Classes

How to write ASP Classes

(ASP Classes) for IIS. ASP Class might intimidate those who are not familiar with Class or Object Oriented Programming (OOP). Writing an ASP Class makes your Code portable, like a Function does, but it also creates a separate work space, variables, etc. Class is not for everything, and over engineering DOES occur. You should use Class where Class "Makes Since". Lets take a look at a simple Class

Issues:Classes must be Created and should be destroyed after usage.

Syntax: Create Class
Start your Class
Class Your_First_Class "This Starts the Class
Declare Public Variables, they can be accessed from outside and inside the Class.
Public External_Number
Declare Private Variables, they can only be accessed from outside the Class.
Private Internal_Number
Public Subs and Functions can be accessed from inside and outside of the Class.
Public Sub Add_One
"Subs can not return data.
"This Sub can be called from Inside or Outside of the Class
External_Number = External_Number + 1
End Sub
Public Function Add_Five
"This Function can return data Outside of the Class
"This Function can be called from Inside or Outside of the Class
Add_Five = External_Number + 5
End Function
Private Subs and Functions can only be accessed from inside of the Class.
Private Sub Add_Ten
"This sub can not return data.
"This Sub can only be called from Inside of the Class
End Sub
Private Function Add_100
"This Function can return data only to inside of the Class
"This Function can be called from Inside or Outside of the Class
End Function
Special Class Sub.
Private Sub Class_Initialize
"This Sub ONLY is valid in a Class
"This Sub is called every time an instance of the Class is Created
End Sub
End your Class
End Class

Syntax: Call Your Class
Create an instance of your Class
Dim Class_Instance
Set Class_Instance = New Your_First_Class
Now you can access Public Variables of your Class
Class_Instance.External_Number = 5
NOTE: We no longer refer to the Class by its name "Your_First_Class"
We now refer to it by Class_Instance.[Method][Property]
Lets get the External_Number back out of the Class
Response.Write Class_Instance.External_Number
Lets access one of the Classes Subs (Public Only)
Class_Instance.Add_One
Response.Write Class_Instance.External_Number
Lets access one of the Classes Functions (Public ONLY)
Response.Write Class_Instance.Add_Five
The Private Subs , Functions, and Variables can not be accessed from here. They are for Intra Class use.

Note: Classes are usually kept in #Includes named .asp or something safe. Please read my article on #Includes.

Example ASP: (In Code window below also)