To use class _asp class classes in VBScript

Source: Internet
Author: User
Tags error handling tidy
First, before I go into the substantive topic and explain how to create a class, I want to make sure you know "object." Although you can use objects in a program without having to know the correct rules, I don't recommend it! For beginners of the object, the next section will let you understand its concepts and content. Readers who already understand object-oriented programming (OOP) can skip this chapter.
Introduction
L "What is the object?" "--an object usually represents an entity, primarily a collection of variables and functions.
L "What is the entity?" "--literally, the entity is a" thing, "I mean a concept or any object. For example, a car is an entity because it is an object. Sales of your company's sales department is also an entity, of course, you can also take it apart, sales people, customers, products, etc. are entities.
Let's look at the "Sales" Entity (object) in more depth. In order to make you more accurate to have a sales "image", you need to know what the customer bought, which customer, who is the salesperson and so on ... This seems like a simple event, but assuming all the information is stored in a separate database table, then when you need to get all the relevant information about a sales process, you have to do multiple independent queries in your database and gather all the data. Is there a more convenient way to get all the information about the sale at once? "Object".
In an object, you can embed code to get data from other tables, and you can save all the information about the object's properties so that you can easily manage your sales data using code. For example:
"" "Open The database connection
Set objconn = Server.CreateObject ("ADODB. Connection ")
objConn.Open "MyDSN"
"" "Create the Recordset object
Set objRS = Server.CreateObject ("ADODB. Recordset ")
"" ' Define the SQL query
Strcomplexsqlquery = "Select C.name, s.name from Customers C," & _
"Salespeople S, Sales Sl WHERE sl.customerid=c.id and" & _
"Sl.salespersonid=s.id and Sl.id=" & Stridofthissale & ";"
"' Open ' recordset
Objrs.open strcomplexsqlquery, objconn, adOpenForwardOnly, _
adLockReadOnly, adCmdText
"' Take the customer and sales person names from the recordset
Strcustomername = objRS (0)
Strsalespersonname = objRS (1)
"" "Tidy up the objects
Objrs.close
Objconn.close
Set objRS = Nothing
Set objconn = Nothing
"" "" Output The Data
Response.Write "This sale is made by" & Strsalespersonname & _
"To" & Strcustomername
You can use objects to override the following:
' Create the ' Sale ' object
Set Objsale = New Sale
"' The correct sale
Objsale.id = Stridofthissale
"" "" Output The Data
Response.Write "This sale is made by" & Objsale.salespersonname & _
"To" & Objsale.customername
"" "Tidy up the objects
Objsale.close
Set Objsale = Nothing
If you use "Sale" objects to do more than print, you can save a lot of typing time.
In calculations, the object includes properties and methods. A property is primarily a variable stored in an object, with the same usage as a variable. The only difference is that the parameter assignment is: Strmyvar = ' This is a string variant ' and the object property is Objobject.property= ' This is a string variant '. This is a very simple and useful place. Method can be understood as the function and process in the implanted object, and the Strmyvar = Objobject.methodname (Strmyvar) can be used instead of Strmyvar =functionname (Strmyvar). The writing is different, but the function is the same. An example of an attribute is the Expireabsolute in Object Response, Response.ExpiresAbsolute = CDate ("1 September 1999"). An example of the method is the Write method in Object Response, Response.Write "Hello world!".
A new feature of VBScript is that it can create new objects without requiring a compiler that is extremely time-consuming. I'll show my readers how to create classes for objects and hopefully provide a good start.
Creating objects
When you create an object type (Class) in VBScript, you first have to know that it's really easy! I self-study in an afternoon, just read the Microsof VB Script reference book, but must admit that this book is not the easiest document to read.
Beginners need to install the VBScript 5.0 engine, which can be downloaded at Microsoft's scripting site.
Let's look at the code. The definition of a class is very similar to a function and a child procedure. The start behavior class, ending with End class, all object definitions written in the intermediary department. Now we can build the first class with what we've learned and not implement any functional classes.
Class 4GuysTestObject
End Class
This doesn't seem to be the case, but when you write the following code, you create an instance of the object:
Dim Objtestobject
Set Objtestobject = New 4GuysTestObject
Set Objtestobject = Nothing
Obviously, we can't do anything with objects right now, and now I'm going to explain how to define properties and methods in the object.
The most basic thing you can do with an object is to create a set of data. For example, if you want to set up a time, date, and video program title, you can create an object that contains the attributes "StartTime", "Programdate", and "Programtitle". The code is as follows:
Class Tvprogram
Public StartTime
Public programdate
Public Programtitle
End Class
Dim Objtvshow
Set objtvshow = New Tvprogram
Objtvshow.starttime = CDate ("17:30")
Objtvshow.programdate = DateSerial (1999,9,17)
Objtvshow.programtitle = "The Jerry Springer Show"
Response.Write Objtvshow.programtitle & "are on" & _
Objtvshow.starttime & "on" & Objtvshow.programdate
The way the code works is that we define the properties of Starttime,programdate and Programtitle as Class Tvprogram. In this way, these properties are processed like other variables, and the code is not executed without a set value. The public field before the property name has its true meaning and is very important. If you do not specifically refer to a method or property as public or private, the system defaults to public, but it is best to develop a good writing habit that defines any value (and also facilitates your own reading later).
The results of the above program are roughly as follows (depends on your local server configuration):
The Jerry Springer show are on 17/09/99 in 5:30pm.
I am in England, so the date is realistic as above. No matter what project you run, it works fine, but only if you start using the functions of other objects, create a perfect interface for all the information and functionality you might need, to support the entities that surround you, and you will realize the real power of the object.
Now, if you don't like the way the above example shows a date, and you want to have a realistic date in the same format, and you don't need to add formatdatetime () to each programdate attribute, you just embed the code into the attribute itself.
This requires a different method to define the attribute. Again, we will use programdate for externally visible properties, but because the Programdate property becomes a function instead of a static value, we save the actual date in another property internal_programdate.
Class Tvprogram
Public StartTime
Public internal_programdate
Public Property Get Programdate
Programdate = Day (internal_programdate) & _
"" & MonthName (Month (internal_programdate)) & _
"" & Year (Internal_programdate)
End Property
Public Programtitle
End Class
Dim Objtvshow
Set objtvshow = New Tvprogram
Objtvshow.starttime = CDate ("17:30")
Objtvshow.internal_programdate = DateSerial (1999,9,17)
Objtvshow.programtitle = "The Jerry Springer Show"
Response.Write Objtvshow.programtitle & "are on" & _
Objtvshow.starttime & "On" & Objtvshow.programdate & "."
The results of the procedure are as follows:
The Jerry Springer Show are on September 1999 5:30pm.
Let's analyze the program in (2):
Class Tvprogram
Public StartTime
Public internal_programdate
Public Property Get Programdate
Programdate = Day (internal_programdate) & _
"" & MonthName (Month (internal_programdate)) & _
"" & Year (Internal_programdate)
End Property
Public Programtitle
End Class
Dim Objtvshow
Set objtvshow = New Tvprogram
Objtvshow.starttime = CDate ("17:30")
Objtvshow.internal_programdate = DateSerial (1999,9,17)
Objtvshow.programtitle = "The Jerry Springer Show"
Response.Write Objtvshow.programtitle & "are on" & _
Objtvshow.starttime & "On" & Objtvshow.programdate & "."
When you call the object's property programdate, you actually perform the function programdate, which is the function defined on it, and soon you will also get used to the way the public or private keyword is used in the declaration section. The keyword "Property" tells the compiler to call the function externally, as it would call the attribute. The next "Get" indicates whether the function is an output or a value.
Get means "Allow external code to ' fetch ' a value ' and its similar keyword has" let "and" Set ", but these two are more complicated, so we'll discuss it later.
The next code looks a bit difficult, assigning a value to objectname.internal_programdate and calling it through Objectname.programdate. Wouldn't it be better if you could use the same keyword to assign a value to it and get its value? Of course, that's OK.
If you define the same names for the Get and let properties, you can treat them as objects with the same properties, but only if they define the same number of members. (The following code appears to be different, only as an instance reference)
Class Tvprogram
Public StartTime
Public internal_programdate
Public Property Get Programdate
Programdate = Day (internal_programdate) & "" _
& MonthName (Month (internal_programdate)) & _
"" & Year (Internal_programdate)
End Property
Public Property Let Programdate (ByVal Vardatein)
Internal_programdate = CDate (Vardatein)
End Property
Public Programtitle
End Class
Dim Objtvshow
Set objtvshow = New Tvprogram
Objtvshow.starttime = CDate ("17:30")
Objtvshow.programdate = "Sept 99"
Objtvshow.programtitle = "The Jerry Springer Show"
Response.Write Objtvshow.programtitle & "are on" & _
Objtvshow.starttime & "On" & Objtvshow.programdate & "."
The Let's declaration section in the above code appears to be an extra element that I studied for a long time when I first saw it. Every time I use "0" as a variable on each attribute, I always get this error message, "the number of elements must be equal." "They are indeed equal!" After being mad, I looked back at the program before I felt stupid! :)
The reason is that when you try to assign a value to a programdate, you use one line of programs:
Objtvshow.programdate = Dtmmydate
For convenience, the value to the right of the equal sign (here refers to dtmmydate) as a Cheng gives the function. So the compiler may think that there are 0 programdate in the GET, and let Programdate is one more! The assigned value is always skipped and used as the last pass of the attribute, so even if you use a different element, the assigned value is always the last one.
Now look at the program. Regardless of whether the date is formatted as text by programdate or translated into a date variable with internal_programdate, the program has no problem. But can not only use one entrance?
If internal_programdate can only be valid internally, and we use let programdate to check the data type of the transmission, we can make a choice. For example:
Class Tvprogram
Public StartTime
Private internal_programdate
Public Property Get Programdate
Programdate = Day (internal_programdate) & "" & _
MonthName (Month (internal_programdate)) & _
"" & Year (Internal_programdate)
End Property
Public Property Let Programdate (ByVal Vardatein)
If IsDate (Vardatein) Then
Internal_programdate = Vardatein
Else
"' Place some error handling code in here.
End If
End Property
Public Programtitle
End Class
And also declares the StartTime property:
Class Tvprogram
Private Internal_starttime
Public Property Get StartTime
StartTime = Hour (internal_starttime) & ":" _
& Minute (Internal_starttime)
End Property
Public Property Let StartTime (ByVal Vartimein)
If IsDate (Vartimein) Then
Internal_starttime = Vartimein
End If
End Property
Private internal_programdate
Public Property Get Programdate
Programdate = Day (internal_programdate) & "" _
& MonthName (Month (internal_programdate)) & _
"" & Year (Internal_programdate)
End Property
Public Property Let Programdate (ByVal Vardatein)
If IsDate (Vardatein) Then
Internal_programdate = Vardatein
End If
End Property
Public Programtitle
End Class
...
Now the code is somewhat less practical than we want, and we'll use class Tvprogram on other pages, so it's best to define it separately so that all faces can be invoked. We will discuss this in part fourth.
Now the code is somewhat less practical than we want, and we'll use class Tvprogram on other pages, so it's best to define it separately so that all faces can be invoked. Create an ASP page and name it tvprogramclass.asp, where we define class Tvprogram.
--tvprogramclass.asp--
<%
Class Tvprogram
Private Internal_starttime
Public Property Get StartTime
StartTime = Hour (internal_starttime) & _
":" & Minute (Internal_starttime)
End Property
Public Property Let StartTime (ByVal Vartimein)
If IsDate (Vartimein) Then
Internal_starttime = Vartimein
End If
End Property
Private internal_programdate
Public Property Get Programdate
Programdate = Day (internal_programdate) & _
"" & MonthName (Month (internal_programdate)) & _
"" & Year (Internal_programdate)
End Property
Public Property Let Programdate (ByVal Vardatein)
If IsDate (Vardatein) Then
Internal_programdate = Vardatein
End If
End Property
Public Programtitle
End Class
%>
This allows you to call our defined classes in any ASP, as follows:
<!--#include virtual= "tvprogramclass.asp"-->
<%
Dim Objtvshow
Set objtvshow = New Tvprogram
Objtvshow.starttime = CDate ("17:30")
Objtvshow.programdate = DateSerial (1999,9,17)
Objtvshow.programtitle = "The Jerry Springer Show"
%>
<%= Objtvshow.programtitle%> is on in <%= objtvshow.starttime%> on <%= objtvshow.programdate%>.
    here's a suggestion. If you rename your include file. asp and make sure that all the important code is in <CODE><% ... %>< CODE>, then even if someone guesses the name of the file you included, There is no way to see the contents of the inside!  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.