What I wrote before:
Everyone who writes programs knows that when you gradually develop the functions you want to implement, most of the time, what you wrote on the first day will be forgotten the next day. Many times, you have to write Detailed program development notes are provided. This is especially true in ASP system development when files and functions are complex. When we plan to modify some functions of the website, we feel that we have no idea where to start or what we feel needs to be modified. At this time, if you have learned any object-oriented programming language, you will naturally think of how to implement the code function into modules. ASP is not object-oriented programming in nature, but VBSCRPIT6.0 provides classes. We can use classes Realize code encapsulation and implement module language.
First of all, I want to write down some very official concepts here, which are intended to illustrate that object-oriented is a very concrete and substantial model, 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 this way: an object is a self-contained entity identified by a set of identifiable properties and behaviors.
In object-oriented programming (oop) programming, the following two terms are used.
Class: This is the template of an object and defines the characteristics of the object.
Instance: This is a real object, something you can interact with.
Properties, methods and events
In OOP, the following terms describe the characteristics of objects:
Attribute: This is a ranking that describes the attributes of an object.
Method: This is a verb that describes what an object can do, or what it is expected to do.
Event: describes the operation performed by an object in response to an action.
Objects are part of object-oriented programming and object-oriented design when programming. They have very great advantages. Many people think that this is a complicated subject, but in fact, it is very simple and can be explained in four simple terms. : Abstraction, encapsulation, polymorphism and inheritance.
Abstraction: This is a way to hide the complexity, the inner workings of a class, so the user doesn't have to know how it works, just like that. If you want to watch TV, you don't need to know how the TV works. You just need to turn on the TV and search for channels. The on/off switch abstracts the actual operation. In the string example, there is a trim method, which can delete For the space at the end of the string, you also don't need to know how it accomplishes this task, you just need to 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 operations. In the term TOupper() method, string does not have to get information from other places to complete its operations. All characters are converted to uppercase.
Polymorphism: This term is used to indicate that different objects can perform the same action, but through their own implementation code. The name is the same, but the underlying implementation code is different.
Inheritance: It defines how classes relate to each other and share characteristics. The way inheritance works is to define classes and subclasses, where the subclass inherits all the characteristics of the parent class. The importance of inheritance is that it forces classes with similar types to have Consistency and allows sharing of code without having to define all the characteristics of the parent class if one decides to create a new class.
Use classes in ASP to achieve modularity
Let me explain it by giving a few simple examples. Note that what is emphasized here is an idea. If you can use a class (base class) to expand when you develop an ASP website, this is very necessary (also Very difficult).
Let’s choose a simple example first:
We want to display the information of classic forum users. After entering the user's ID, some information of the user can be displayed. This is a process. You can think of it this way. We treat the user as an object. His attributes are ID and gender. ,Points, permissions, the implementation method is to display this information, ok, write like this:
Class blueidea
Private bname,bpoint,bsex,blevel
'........................
end class
Here we first declare a class named blueidea, followed by some private variables to store the properties of the blueidea class. These variables cannot be accessed from outside the code. This is data protection. To define these variables, use the property statement to obtain the value. Indirectly pay 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
'------------------------------------------------ ------------------
There is a killint function here to determine the validity of the data. Its prototype 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 of this function is very clear and is no longer complicated.
Since we need to judge the user level through points, 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 send back the user's information, we must define a public function to display the information:
Public Function showuser()
response.write(<h5>The following shows the information of <font color=red>&bname&</font>:</h5>)
response.write(<h5>Gender:<font color=red>&bsex&</font></h5>)
response.write(<h5>Points:<font color=red>&bpoint&</font></h5>)
getlevel
response.write(<h5>Level:<font color=red>&blevel&</font></h5>)
End Function
End class
When using this class, use it like this: (I wrote a form processing here)
Set blueideauser=new blueidea
blueideauser.getname=Trim(request(id))
blueideauser.getsex=request(sex)
blueideauser.getpoint=request(point)
blueideauser.showuser
If you want to see the effect, then take a look here: http://www.5do8.com/net/aspclass/class.asp
Class that controls reading database information:
Reference source code
'Name: ado_5do8
'Function: Read various operations of the database
'Source-Cultivated Village http://www.5do8.com http://www.Blueidea.com-5do8
'Creation: 5do8
'Contact:[email protected]
'Update: November 13, 2005
'Authorization: Blue Ideal website points exceed 3,000, all registered users of Gengyun Village
'Interface of class: ado_5do8.ConnectString=absolute path to 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 eliminates 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=server.CreateObject(adodb.connection)
strconn=Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & Server.MapPath(sqlstr)
conn.open strconn
If Err Then
err.Clear
SetConn=Nothing
mess=An error occurred and cannot connect to the database
response.write(mess)
response.End
Else
mess=Connected to database conn successfully.......<br/>
response.write(mess)
End If
End Sub
'------------------------------------------------ ---------------
private Sub closeconn()
conn.close
Set conn=Nothing
response.write(<strong style='color:red'>Close conn connection</strong>...<hr/>)
End sub
'------------------------------------------------ ----------------
Private Sub closers()
rs.close
Set rs=Nothing
response.write(<strong style='color:#085420'>Close database RS</strong>.....<br/>)
End Sub
'------------------------------------------------ ----------------
Property Get possession
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 GetConnectString
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)
table = 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)
response.write(<h5> total number obtained:)
response.write(<b> &iRows+1&</b> messages</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
Response.Write (<li style='padding:4px 0;'><a href=?k=read&rowid=&irowloop+1&>&data(1, iRowLoop) & </a><span style='padding:4px 0 4px 10px;background-color:#ccc; '>Slower, not recommended to click--><a href=?k=list&id=&data(0,irowloop)&>update</a></span></li>)
Next
Response.Write </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=default.asp>Return to typical mode</a>):
if Start > 0 then
Response.Write <A HREF=?k=read&Start= & iStart-ipageno &&pageno= & ipageno & >Previous</A>
end if
if iStop < iRows then
Response.Write <A HREF=?k=read&Start= & iStart+ipageno &&pageno= & ipageno & >Next</A>
endIf
response.write</div>
Else
rowid=litter_in(rowid-1,iRows)
response.write(<div style='width:85%'><h4 style='text-align:center'><a href=?k=read&pageno=&pageno&&start=&start&>Return list</a></h4></h2><hr/><h5>&server.htmlencode(data(1,rowid))&</h5><p >&server.htmlencode(data(2,rowid))&<h5>+-----&server.htmlencode(data(3,rowid))&)
response.write(<div>)
End if
End Function
'------------------------------------------------ ----------------
Public Function list_ids()
sql3=select * from &itable& where id=&iid&
startconn()
Set rs=conn.execute(sql3)
If rs.eof And rs.bof Then
data=Array(0,0,0,0)
Else
data=Rs.GetRows()
End If
closers
closeconn
response.write(UBound(data)&:)
response.write(server.htmlencode(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=conn.execute(sql2)
szd_count=rs(szd_count)
closers
Set rs = Conn.Execute(sql)
dim data
If Rs.Eof Then
data=no data
Else
data=Rs.GetRows()
End if
closers
closeconn()
Call makes session (data)
End Function
'++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++
End Class
When using:
Dim action
action=request(k)
If action=view Then
Call viewnew
ElseIf action=list Then
Call list()
ElseIf action=read Then
Call read()
Else
Callff()
End if
Subff()
%>
<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 amount of information displayed:<input name=n type=text maxlength=4 size=10 />Number of pages per 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
cs.ConnectString=data/a.mdb
cs.havese=shi
cs.rs_top f_num,site_szd,
cs.readarraysession arrstart,pagesize,rowid
End sub
Sublist()
response.write(<h5><a href=default.asp>Return to default mode</a></h5>)
Specific information is displayed below response.write:<hr/>
id=request(id)
id=killint(id,1,1)
Set listid=new ado_5do8
listid.ConnectString=data/a.mdb
listid.getid=id
listid.gettable=site_szd
listid.list_ids()
End Sub
Subread()
response.write<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=default.asp>Return to typical mode</a>Parameters: Start, start element; pageno, number of items 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
cs.havese=shi
cs.readarraysession 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
%>