Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to organize development using classes in VBScript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use classes in VBScript to organize development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

When creating Web applications within an organization, you sometimes find yourself copying and pasting the same code from one application to another. It will be much more convenient if you can solidify all these processes into a handy package, and then you can use it throughout the domain.

At the same time, if you have a reuse process, it may be useful to create a class that contains your process. Using classes to organize your code has some advantages: it makes your code easy to read and debug; you can easily transfer your classes to a Web service; you provide other developers with an abstract tool (which saves time and money); and you retain flexibility during the development phase of a project.

When you organize your common processes into a class, you create a level of abstraction that allows you to implement them in almost all code. For example, suppose that in every Web application you create, you need to initialize user information. This initialization process includes some database server connections based on the user's credentials when the user logs in to your application. If you need to do this in every application, it makes sense to solidify your code into a class.

Here is how to implement it:

Dim MyToolbox

Set MyToolbox = New CToolbox

Response.Write "UserName:" & MyToolbox.UserName &

"& vbCrLf

Response.Write "UserPhone:" & MyToolbox.UserPhone &

"& vbCrLf

Set MyToolbox = Nothing

Class CToolbox

Private m_conn, m_rs

Private m_username, m_userphone

Public Property Get UserName ()

UserName = m_username

End Property

Public Property Get UserPhone ()

UserPhone = m_userphone

End Property

Private Sub Class_Initialize ()

Set m_conn = Server.CreateObject ("ADODB.Connection")

M_conn.ConnectionString = "Some connection string"

M_conn.Open

Set m_rs = Server.CreateObject ("ADODB.Recordset")

Set m_rs.ActiveConnection = m_conn

M_rs.Open "Select * FROM Users Where userid ='" &

Request.ServerVariables ("LOGON_USER") & "'

If Not m_rs.EOF Then

M_username = m_rs.Fields ("username")

M_userphone = m_rs.Fields ("userphone")

End If

End Sub

Private Sub Class_Terminate ()

On Error Resume Next

M_rs.Close

Set m_rs = Nothing

M_conn.Close

Set m_conn = Nothing

End Sub

End Class

You can see from the example that the UserName and UserPhone properties have values after the class is initialized. You can put the code of this class at the end of the ASP code. The functionality can then be used in the rest of the code without instantiating the ADO object for database calls and subsequent cleanup. Also, if you are using Microsoft Visual InterDev, the properties and methods defined in the class can be used through IntelliSense.

IntelliSense is only valid for first-level properties and methods, so if you create a class that returns objects of other ASP classes, InterDev will not provide available methods and properties.

The following code is the skeleton of a class:

Class MyClass

Private Sub Class_Initialize ()

End Sub

Private Sub Class_Terminate ()

End Sub

End Class

With this basic information, a class is declared and can be used to create objects. But the objects of this class have no function-they are useless. The methods of the class are built from Public Sub or Public Function declarations (Subs does not return a value, while Function does).

There are two ways to expose properties: you can use either Public VarName or Public Property Get/Set/Let in the global context of the class. If you need a read-only property, simply use a Property Get declaration. All other Properties,Subs or Functions declared as Private are available only on instantiated objects. Variables declared as Private in the global context of the class are member variables.

Another benefit of creating a class is that you can easily transfer the class declaration to the Web service. Because this is where many developers are going, you can go first by using classes to create object-oriented code. When you transfer these classes to a Web service, you do not need to rewrite all the ASP code that places that code. The only change you need to make is the instantiation of your object.

I have found that the biggest advantage of creating this abstraction is that you can use the help of other Web developers. Because it is quite inefficient for many developers to repeatedly create the same code in each different implementation, this approach provides an abstract tool for each developer. This allows each developer to focus more on the functionality that he or she is currently working on, rather than spending time rewriting the code they have developed.

If you're wondering why I don't provide a COM object to handle this function, it's because it locks you into the detailed design. By creating a class dynamically in ASP, you can make changes to the class without recompiling and publishing it. After the class is fixed, move it to COM, Windows scripting component (WSC), or a Web service, but it's better to be flexible during the development phase.

Thank you for reading! This is the end of this article on "how to use the classes in VBScript to organize development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report