ASP Object-Oriented Programming

Source: Internet
Author: User

Discussion on ASP Object-Oriented Programming and comparison of blogs selected from RAINMAN_NET
Keyword ASP object-oriented programming discussion and Comparison
Source

ASP is a dynamic web page Programming Technology launched by Microsoft earlier, however, with the help of ADO, its convenient and convenient access to databases, XML, COM/ActiveX, and other technologies, the multi-layer server architecture has made it still have a tenacious vitality today, there are still some developments. Although ASP. Net is totally different from ASP in architecture, many of its built-in objects are also extended based on ASP. There are countless articles on ASP on the Internet, but few articles about ASP object-oriented and comparison with other languages, that is why I made up my mind to write this article.
Because it is an earlier version, ASP only provides very weak object-oriented interfaces. As we all know, ASP implementation languages are divided into VBScript and JavaScript/JScript: In VBScript, there are Class keywords that can be used to declare a custom Class. JavaScript is quite strange, it uses a function to "declare" the class, and then uses this. prototype defines attributes, this. func definition method. We will discuss VBScript as the main component here. The class declaration of VBScript is as follows:
Class name
Statements
End Class
Here, statements can declare public or private members, including functions, members, and attributes. For attributes, I had to like Microsoft's get and set methods. This concept appeared in COM. net has been used for a long time. I personally think it is much easier and more intuitive for programmers to achieve the same effect than using getProp () and setProp () methods in Java.
In contrast, the classes in VBScript have their own merits (of course, they cannot be compared with the latest PHP5). The classes in VBScript maintain the incomplete object-oriented "features" of VB ", it only implements the most basic constructor/destructor, member functions, variables, attributes, and even constructors without parameters. In PHP4, the important properties of classes such as inheritance and function overloading are also implemented. Only when these attributes are implemented can they be called object-oriented, which may provide the basis for implementation of polymorphism. However, neither of them implements static (static) member functions of the class. Although some other alternatives can be used to achieve the same effect, this is not thorough from the perspective of Object-oriented Thinking (because PHP is very flexible, in PHP4, static variables of the class can be indirectly implemented through static variables of the member function; and ":" -- the operator that can implement static function access of the class -- no strict check in PHP4. In other words, all member functions can be accessed as static functions, so long as you do not use member variables in the function, there will be no errors. VBScript does not implement static at all, and can only be implemented using Session or Application ). Therefore, in normal use, you can use the custom class of VBScript to encapsulate some operations, but do not expect it to be like C ++/Java /. net serves your object-oriented ideology.
VBScript also carries forward the reference style of default parameters or variables in VB. In this way, although the Script language is not sensitive to types, it can achieve the same effect of pointer/reference in C/C ++ and accomplish many things. The most basic, for example, using it to define a List node class ListNode:
<%
Class ListNode
Public Content
Public NextNode

Private Sub Class_Initialize ()
Content = "Node"
Set NextNode = Nothing
End Sub
End Class
%>
It's that simple, but don't feel disdainful or forget the initial value of the variable. In VB, the type is added during declaration. In use:
<%
Set nh = new ListNode
Set nh. NextNode = new ListNode
'Other statements ......
'Traverse the list
Set n = nh
While Not n is Nothing
Response. Write n. Content + "<br/>"
Set n = n. NextNode
Wend
%>
If no other code is added, the preceding running result is two "nodes ". VBScript's custom classes and objects are nothing more than that. As long as you master the basic concepts and have a certain understanding of them, it will be much simpler. Once again, the Set statement is used to assign values to objects, which is equivalent to the value assignment in Java and obtains a reference. This is much better than the default object Value assignment in PHP4 when calling the copy constructor to create a new object (even the statement obj = new Obj; creates two objects! If you want to get a reference, you need to add &) in front of the variable after the equal sign, and it seems that PHP5 does not want to modify PHP4.
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 is intended 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 from the 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.

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.