SoFunction
Updated on 2025-04-09

[ASP] Use classes to implement modularity

All those who write programs know that when you gradually expand on the functions you want to implement, when you are very big, you forget to write what you wrote on the first day the next day. Many times, you have to write detailed program development notes. This is especially when files and functions are complicated in ASP system development. When we plan to modify some functions of the website, we feel that we have no idea where to start or feel that we need to modify them. At this time, if you have learned any object-oriented programming language, you will naturally think of how to implement module language with code functions. Asp is not object-oriented programming in essence, but VBSCRPIT 6.0 provides classes, and we can implement code encapsulation through classes to implement module language.

First of all, I want to write some very official concepts here, which is intended to illustrate that object-oriented is a very concrete and physical pattern, and some people cannot be scared away when they see the "object".

An object is something that can be seen, felt, heard, touched, tasted or smelled. Here we "define" it like this: an object is a self-contained entity identified by a set of identifiable characteristics and behaviors.
In the programming method of object-oriented programming (OOP), use the following two terms.
Class: This is the template of the object, which defines the properties of the object.
Example: This is a real object, something that can interact with.

Properties, methods and events

In OOP, the following terms describe the properties of an object:

Attribute: This is a ranking that describes the properties of an object.

Method: This is a verb that describes the work that an object can do, or what it wants it to do.

Event: Describes the operation performed by the object for a corresponding action.
When programming, part of object-oriented programming and object-oriented design of objects has a very big advantage, which many people think is a complex topic, but in fact, it is very simple and can be explained in four simple terms: abstraction, encapsulation, polymorphism, and inheritance.

Abstract: This is a hidden complexity, the internal workings of the class, so the user doesn't have to know how it works, like. If you want to watch TV, you don’t have to know how the TV works. Just turn on the TV and search for the channel. The on/off switch abstracts the actual operation. In the string example, there is a trim method that can delete the spaces at the end of the string. You don’t need to know how it completes this task, just know that it has this function.

Encapsulation: Each object contains all the information needed to perform operations. This object is called encapsulation, so the object does not rely on other objects to complete its own operations. In the term TOupper() method, string does not have to go to other places to obtain information to convert all characters into capitalization.

Polymorphism: This term is used to indicate that different objects can perform the same actions, but must be executed through their own implementation code, with the same name, but the underlying implementation code is different.

Inheritance: It defines how classes are associated with each other, share features, inheritance works by defining classes and subclasses, where subclasses inherit all features of the parent class. The importance of inheritance is that it forces classes of similar types to be consistent and allows shared code, and if you decide to create a new class, you don't have to define all features of the parent class.

Use classes in ASP to implement modularity

Let me illustrate this by giving a few simple examples. Note that the emphasis here is a kind of idea. If you can use a class (base class) to expand it when you develop an ASP website, it is very necessary (and very difficult).

Let's first choose a simple example:

We want to display the information of the classic forum user. After entering the user's ID, we can display some information of the user. This is a process. We can consider this. We regard the user as an object. Some attributes are ID, gender, points, and permissions. The implementation method shows these information. OK, write this way:

Class blueidea
Private bname,bpoint,bsex,blevel
''''...................
end class

Here we first declare a class named blueidea, followed by some private variables, which are used to store the properties of the blueidea class. These variables cannot be accessed outside the code. This is data protection. To define these variables, we use property statements to obtain values ​​and pay them indirectly to private variables.

''''-----------------------------------------------------------------
Property Get getname
getname=bname
End Property

Property Let getname(nameid)
bname=nameid
If nameid="" Then
bname="No registered user"
End If
End Property
''''------------------------------------------------------------------
Property Get getsex
getsex=bsex
End Property

Property Let getsex(sex)
bsex=killint(sex,0,0)
If bsex=0 Then
bsex="male"
Else
bsex="female"
End if
End Property
''''------------------------------------------------------------------
Property Get getpoint
getpoint=bpoint
End Property

Property Let getpoint(point)
bpoint=killint(point,0,0)
End Property
''''------------------------------------------------------------------

Here is a killint function that judges the legality of the data. Its original shape is:

Private Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function

The function is very clear and no longer tedious.

Since we want to judge the user level through integrals, a private function is defined here:

Private Function getlevel()
bpoint=killint(bpoint,0,0)
If bpoint<500 Then
blevel="Junior Member"
ElseIf bpoint>=500 And bpoint<=100 Then
blevel="Premium Member"
Else
blevel="Ultimate Member"
End If
Getlevel=blevel
End Function

If we want to get the information to be returned to the user, we must define a public public function to display the information:

Public Function showuser()
("<h5>The following information showing <font color=red>"&bname&"</font>:</h5>")
("<h5>Gender:<font color=red>"&bsex&"</font></h5>")
("<h5>Points:<font color=red>"&bpoint&"</font></h5>")
getlevel
("<h5>Level:<font color=red>"&blevel&"</font></h5>")
End Function
End class

When using this class, use it like this: (I wrote a form for processing here)

Set blueideauser=new blueidea
=Trim(request("id"))
=request("sex")
=request("point")


Classes that control reading database information:
Reference source code:

''''Name: ado_5do8
'''' Function: Read all operations of the database
''''Source-Gengyun Village http://-5do8
''''Creation: 5do8
''''Contact: 5do8@
''''Updated: November 13, 2005
''''Authorization: Blue Ideal Website has more than 3,000 points, all registered users in Gengyun Village
Interface of '''' class: ado_5do8.ConnectString=Absolute path of the database
'''ado_5do8.rs_top number of calls, name of the table
Class ado_5do8
Private conn,sqlstr,rs,iid,itable,isession
'''''sqlstr: Database address, absolute path, private
''''conn: Open the database connection, private

''''------------------------------------------------------------------
rem eliminate some unwanted numbers
Private Function litter_in(r1,r2)
If IsNumeric(r1) and IsNumeric(r2) Then
Dim dimrr
If r1>r2 Then
dimrr=r2
Else
dimrr=r1
End If
Else
dimrr=0
End if
litter_in=dimrr
End Function
''''-----------------------------------------------------------------
Private Function killint(i,killstr,killsub)
 If Not IsNumeric(i) Then
 i=killstr
 ElseIf i<=0 Then
 i=killsub
 End if
 killint=Int(Left(i,5))
 End Function
''''-----------------------------------------------------------
private Sub startconn()
  On Error Resume Next 
   Set conn=("")
  strconn="Provider=.4.0;Data Source=" & (sqlstr)
   strconn
  If Err Then
  
  Set Conn = Nothing
mess="Error occurred, the database cannot be connected"
  (mess)
  
  Else
Mess="Conn connection to the database conn successfully.........
"
  (mess)
  End If
  End Sub
''''----------------------------------------------------------------
private Sub closeconn()
   
  Set conn=Nothing
("<strong style=''''color:red''''''>Close the conn connection</strong>...<hr/>")
 End sub 
''''-----------------------------------------------------------------
Private Sub closers()
 
 Set rs=Nothing
("<strong style=''''color:#085420''''''>Close the database RS</strong>.....
")

 End Sub

''''-----------------------------------------------------------------
Property Get havese
 havese=isession
 End Property

Property Let havese(yoursession)
 isession=yoursession
 If yoursession="" Then
 isession="nodef"
 End If
 End Property

''''-----------------------------------------------------------------
Public Function makesession(arraydata)
  If IsArray(arraydata) then
  makear=arraydata
  Else
  makear=Array(0,0,0,0)
  End If
  If isession="" Then
  isession="nodef"
  End if
  session(isession)=makear
  End Function
''''-----------------------------------------------------------------

private Function getsession()
 thisget=session(isession)
 If Not IsArray(thisget) Then
 thisget=Array(0,0,0,0)
 End If
 Getsession=thisget
 End function
''''-----------------------------------------------------------------
Property Get ConnectString 
ConnectString = sqlstr
End Property
Property Let ConnectString(str) 
sqlstr = str
End Property
''''-----------------------------------------------------------------

Property Get getid 
getid = iid
End Property
Property Let getid(id) 
iid = id
End Property
''''-----------------------------------------------------------------

Property Get gettable 
gettable = itable
End Property
Property Let gettable(table) 
itable = table
End Property
''''-----------------------------------------------------------------
''''------------------------------------------------------------------
public Function readarraysession(iStart,ipageno,irowid)
 rowid=killint(irowid,0,0)
 start=killint(istart,0,0)
 pageno=killint(ipageno,5,5)
  data=getsession
 iRows = UBound(data, 2)
 iCols = UBound(data, 1)
("<h5>Total number obtained:")
("<b> "&iRows+1&"</b>Message</h5><hr/><ul style=''''width:100%;'''''>")
 If rowid = 0 then
 If iRows > (ipageno + iStart) Then
 iStop = ipageno + iStart - 1
 Else
 iStop = iRows
 End If
 For iRowLoop = Start to iStop
("<li style='''''padding:4px 0;'''''><a href=?k=read&row>"&data(1, iRowLoop) & " </a><span style='''''padding:4px 0 4px 10px;background-color:#ccc; ''''''>Slower, click--><a href=?k=list&>Update</a></span></li>")
 Next 
"</ul><div style='''top:20px;background-color:#ccc;color:#020;font-weight:bold;bordr-top:2px solid #008;padding:10px 0;color:#b00''''>list(<a href=>Back to typical mode</a>):"
 if Start > 0 then
   "<A HREF=""?k=read&Start=" & iStart-ipageno &"&pageno=" & ipageno & """>Previous</A>"
 end if 
 if iStop < iRows then
  " <A HREF=""?k=read&Start=" & iStart+ipageno &"&pageno=" & ipageno & """>Next</A>"
 end If

 "</div>"

 Else
 rowid=litter_in(rowid-1,iRows)
("<div style='''''width:85%''''><h4 style='''''text-align:center'''''><a href=?k=read&pageno="&pageno&"&start="&start&">Return to the list</a></h4></h2><hr/><h5>"&(data(1,rowid))&"</h5><p>"&(data(2,rowid))&"<h5>+-----"&(data(3,rowid))&"")
 ("<div >")
 End if
 End Function

''''-----------------------------------------------------------------
Public Function list_ids()
 sql3="select * from "&itable&" where  "
 startconn()
 Set rs=(sql3)
 If  And  Then
 data=Array(0,0,0,0)
 Else
 data=()
 End If
 closers
 closeconn
  (UBound(data)&":")
 ((data(2,0)))
 End function

''''-----------------------------------------------------------------
Public Function rs_top(num,table,whe)
 startconn()
 sql="select top "&num&" * from "&table&""
 sql2="select count(*) as szd_count from "&table&" "" "&whe&""
 Set rs=(sql2)
 szd_count=rs("szd_count")
 closers
  Set rs = (sql)
  dim data
 If  Then
 data="no data"
 Else
 data=()
 End if
 closers
 closeconn()
 Call makesession (data)
   End Function
''''+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
End Class

Trial method:
Dim action 
action=request("k")
If action="view"  Then
Call viewnew
ElseIf action="list" Then
Call list()
 ElseIf action="read" Then
Call read()
Else
Call ff()
End if
 Sub ff()
%> 
<form style="border-top:2px solid #008;border-bottom:2px solid #008;margin:auto;background-color:#eee;padding:20px 5px;color:#008;font-weight:bold;"> 
<label>Total number of displayed information:<input name="n" type="text" maxlength="4" size="10" />Number of each page:<input name="pagesize" type="text" maxlength="4" size="10" value="5"/><input name="arrstart" type="hidden" value="0"></label>

<h5 style="border-top:1px solid #000;padding:5px 0"> Operation:<input name="k" type="submit" value="view" //</h5>

</form> <%End sub%>
 <%Sub viewnew()
 f_num=killint(request("n"),1,1)
  pagesize=killint(request("pageno"),5,5)
 arrstart=killint(request("start"),0,0)
 rowid=killint(request("rowid"),0,0)
Set cs=new ado_5do8
="data/"
="shi"
  cs.rs_top f_num,"site_szd",""
  arrstart,pagesize,rowid
     End sub
 Sub list()
("<h5><a href=>Return to the default mode</a></h5>")
"The specific information is displayed below:<hr/>"
   id=request("id")
   id=killint(id,1,1)
   Set listid=new ado_5do8
   ="data/"
    =id
   ="site_szd"
   listid.list_ids()
End Sub

Sub read()
"<div style=''''background-color:#ccc;padding:20px 0;color:080;font-weight:bold;border-bottom:2px solid #008'''''>The page analysis is completed. To update, please select <a href=>Back to typical mode</a> Parameters: Start, start element; pageno, number of pages per page</div>"
  pagesize=killint(request("pageno"),5,5)
 arrstart=killint(request("start"),0,0)
 rowid=killint(request("rowid"),0,0)
 Set cs=new ado_5do8
 ="shi"
   arrstart,pagesize,rowid

End sub

Function killint(i,killstr,killsub)
 If Not IsNumeric(i) Then
 i=killstr
 ElseIf i<=0 Then
 i=killsub
 End if
 killint=Int(Left(i,5))
 End Function 
%>

illustrate:

This source code is written separately from 5do8. I have the right to interpret the source code, but it does not guarantee the security of the source code. The user bears any losses themselves. The source code is only limited to communication within the site of Gengyun Village (http://), Blue Ideal () and Dike Forum (http://). Of course, plagiarism is not allowed.