Basic Learning of ASP (VBScript)

Source: Internet
Author: User
Tags class definition php and

First, the ASP (VBScript) class is composed of events and methods (they are members of the composition class!
In the Class block, the member is declared as Private (Private member, which can only be called within the Class) or Public (Public member, which can be called inside and outside the Class) through the corresponding declaration statement ). Declared as Private will only be visible within the Class block. Declared as Public is not only visible inside the Class block, but also visible to code outside the Class block. If Private or Public statements are not used, the default value is Public. The process (Sub or Function) declared as Public within the block of the class will become the method of the class. Public variables will become attributes of the class, the same as those explicitly declared using Property Get, Property Let, and Property Set. The Default attributes and methods of classes are specified with the Default keyword in their declaration section.

Let's take an example.

<Script language = vbscript runat = server>

Class myClass
'// ---- Declare (Declaration is defined) the internal (Private [Private]) variable of the myClass class
Private strAuthor
Private strVersion
Private strExample

'// --------------------------- Define the class event -------------------------------//
'// ---- Class_Initialize () is the class initialization event. If you use this class at the beginning, the execution of this part is triggered first, next we will initialize the author and version of the class in this member and display on the screen that the class has started.

Private Sub Class_Initialize ()
StrAuthor = "thinking source"
StrVersion = "1.0"
Response. Write "<br> myClass started <br>"
End Sub
'// ---- Class_Terminate () is the end event of the class. Once the class is exited, the event is triggered, next we will set this event to exit this class, and it will show on the screen that this class has ended.

Private Sub Class_Terminate ()
Response. Write "<br> myClass ended <br>"
End Sub

'// --------------------------- User-defined method -------------------------------//

'// ---- This method returns a version.

Public Sub Information ()
Response. Write "<br> Coding By <a href = 'mailtcoder @ sinobe.com '> Maxid_Zen </a> @

<A href = 'http: // www.design60s.com '> www.design60s.com </a>. <br>"
End Sub

'// --------------------------- Define the output attribute of the class -------------------------------//

'// ---- Specifies the attributes of the class. This attribute allows the user to initialize the strExapmle variable.

Public Property Let setExapmle (ByVal strVar)
StrExapmle = strVar
End Property

'// --------------------------- Define the output attribute of the class -------------------------------//

'// ---- Defines the attributes of the class. This attribute returns a version number.


Public Property Get Version
Version = strVersion
End Property

'// ---- Defines the attributes of the class. This attribute is the author number of the class returned.

Public Property Get Author
Author = strAuthor
End Property

'// ---- Defines the attributes of the class. This attribute returns a version number.

Public Property Get Exapmle
Exapmle = strExapmle
End Property

End Class

</Script>
<%

'// ------- Here is an example of using this class

Dim oneNewClass

Set oneNewClass = New myClass

Response. Write "Author:" & oneNewClass. Author & "<br>"
Response. Write "Version:" & oneNewClass. Version & "<br>"

OneNewClass. setExapmle = "this is an example of a simple class"

Response. Write "custom:" & oneNewClass. Exapmle & "<br>"

OneNewClass. Information

 

Set oneNewClass = Nothing

%>

Sessions in ASP can store objects. They can store basic variables, arrays, and Automation objects. However, they may encounter problems when storing custom class objects.

The following code:
<%
If isempty (Session ("node") Then Set Session ("node") = New ListNode
Set n = Session ("node ")
Response. Write n. Content
%>

Or the above ListNode class. This code intends to keep only one ListNode object in a user session. Therefore, when a user accesses the webpage for the first time, an object of ListNode is generated and saved in Session ("node"). When a user accesses the webpage later, the Session ("node ") it is not empty, so a new object will not be generated, but the saved object will be retrieved in Session ("node.

In theory, it should also output 100, but the problem is that ASP always reports an error:
Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method: 'N'. Content'
An error occurs when n. Type is used. The same code is translated into PHP and can be run through. Why?

After my personal analysis, I think it is correct that the Session can save objects, but the type conversion mechanism in VBScript is too weak and there is no explicit forced type conversion for users, the Session ("node") cannot be correctly converted to the ListNode type. Because it is a custom class, we can only display the class definition statement on each page. In this way, in ASP's view, each time you read this page, the ListNode class is a new class, therefore, we do not recognize the objects of this class in the Session.

Conclusion: Do not use Session or Application to store ASP custom class objects. If necessary, you can use COM to compile the class, and then use Set Session ("obj") = Server in VBScript. createObject ("YourApp. yourClass ") to create an object, and then implement the functions as expected above.

Related Article

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.