VBS类的使用方法
1
vbs类的使用方法
2
类的结构:
3
class 类名称
4![](/Images/OutliningIndicators/None.gif)
5
end class
6
调用方法 set myclass=new 类名称
7
中间类里面的代码,
8![](/Images/OutliningIndicators/None.gif)
9
类模块的事件过程。
10
Private Sub Class_Initialize()
11
这里写上类实例化的时候执行的代码。
12
end sub
13
Private Sub class_terminate()
14
这里是类注销的时候自动执行的代码,
15
end Sub
16
上面两个子程序的名称是规定的,
17
如果改了名称,就会无效。
18
1、变量
19
2、函数(Function)
20
3、属性(Property)
21
4、方法 (SUB)
22![](/Images/OutliningIndicators/None.gif)
23
一、对象的访问限制,分为公有和私有,公有的(public)可以让外部访问,私有的(Private )外部不能访问,
24
上面几种角色都可以定义为公有或私有。
25![](/Images/OutliningIndicators/None.gif)
26
定义变量
27
public mystr,mystring
28
这样就定义了两个公有的变量,调用方法myclass.mystr
29
反之
30
Private mystr,mystring
31
则为私有的变量,外部不能访问的。
32![](/Images/OutliningIndicators/None.gif)
33
函数
34![](/Images/OutliningIndicators/None.gif)
35
Public Function Checkstr(Str)
36
If Isnull(Str) Then
37
CheckStr = ""
38
Exit Function
39
End If
40
CheckStr = Replace(Str,"'","''")
41![](/Images/OutliningIndicators/None.gif)
42
End Function
43
这是一个公有函数,外部可以访问的,调用方法 myclass.Checkstr(Str)
44
反之如果为私有的,则用Private 取代public
45
3、属性(Property)
46
属性分两种,一种是只写属性,一种是只读属性。
47![](/Images/OutliningIndicators/None.gif)
48
也可以为公有或私有,现在以公有为例子。
49
这是一个只写的属性
50
Public Property Let Value(ByVal vNewValue)
51
Dim tmpstr
52
tmpstr = vNewValue
53
tmpstr = split(tmpstr,"@@@")
54
html = split(tmpstr(0),"|||")
55
Strings = split(tmpstr(1),"|||")
56
pic = split(tmpstr(2),"|||")
57
End Property
58
这是一个只读的属性
59
Public Property Get TodayNum
60
TodayNum = Application(Forum_sn & "_Dv_setup")(9,0)
61
End Property
62![](/Images/OutliningIndicators/None.gif)
63
如果要有读写的属性那么
64
Public Property Get TodayNum
65
TodayNum = Application(Forum_sn & "_Dv_setup")(9,0)
66
End Property
67
Public Property Let TodayNum(ByVal vNewValue)
68
TodayNum = vNewValue
69
End Property
70![](/Images/OutliningIndicators/None.gif)
71
这样写TodayNum就具有读写的属性了,
72![](/Images/OutliningIndicators/None.gif)
73
方法,也以公有为例,
74
Public Sub TrueCheckUserLogin()
75
代码
76
end sub
77![](/Images/OutliningIndicators/None.gif)
78![](/Images/OutliningIndicators/None.gif)
79
以下为一个简单的类的的例子:
80
Class cls_templates
81
Public html,Strings,pic
82
Private Sub Class_Initialize()
83![](/Images/OutliningIndicators/None.gif)
84
end sub
85
Private Sub class_terminate()
86![](/Images/OutliningIndicators/None.gif)
87
end sub
88
Public Property Let Value(ByVal vNewValue)
89
Dim tmpstr
90
tmpstr = vNewValue
91
tmpstr = split(tmpstr,"@@@")
92
html = split(tmpstr(0),"|||")
93
Strings = split(tmpstr(1),"|||")
94
pic = split(tmpstr(2),"|||")
95
End Property
96
End Class
97![](/Images/OutliningIndicators/None.gif)
98
使用VBScript 中的类组织开发
99
当在一个机构内部创建Web应用程序时,有时候你会发现你自己从一个应用程序中复制和粘贴相同的代码到其它的应用程序中。如果你能够固化所有这些过程到一个好用的小包(package)中,其后你就可以在整个域中使用它了,这样会方便很多。
100![](/Images/OutliningIndicators/None.gif)
101
同时,如果你有一个重复使用的过程,创建一个类包含你的过程可能会比较有用。使用类组织你的代码有一些优点:它使得你的代码容易阅读和调试;你可以将你的类轻松的移值到一个Web服务中;你为其它开发人员提供一个抽象工具(从而节省了时间和资金);同时在一个项目的开发阶段你还保留了灵活性。
102![](/Images/OutliningIndicators/None.gif)
103
当把你的通用过程组织到一个类的时候,你创建一级抽象,允许你在几乎所有代码中实现这些过程。例如,假设在你创建的每个Web应用程序中,你者需要初始化用户信息。这个初始化过程包括在用户登录到你的应用程序时根据用户凭证所做的一些数据库服务器连接。如果你在每个应用程序中都需要完成这个任务的话,那么将你的代码固化到一个类中相当有意义。
104![](/Images/OutliningIndicators/None.gif)
105
下面是实现方法:
106![](/Images/OutliningIndicators/None.gif)
107
Dim MyToolbox
108
Set MyToolbox = New CToolbox
109
Response.Write "UserName: " & MyToolbox.UserName & "<BR>" & vbCrLf
110
Response.Write "UserPhone: " & MyToolbox.UserPhone & "<BR>" & vbCrLf
111
Set MyToolbox = Nothing
112![](/Images/OutliningIndicators/None.gif)
113
Class CToolbox
114
Private m_conn, m_rs
115
Private m_username, m_userphone
116![](/Images/OutliningIndicators/None.gif)
117
Public Property Get UserName()
118
UserName = m_username
119
End Property
120![](/Images/OutliningIndicators/None.gif)
121
Public Property Get UserPhone()
122
UserPhone = m_userphone
123
End Property
124![](/Images/OutliningIndicators/None.gif)
125
Private Sub Class_Initialize()
126
Set m_conn = Server.CreateObject("ADODB.Connection")
127
m_conn.ConnectionString = "Some connection string"
128
m_conn.Open
129
Set m_rs = Server.CreateObject("ADODB.Recordset")
130
Set m_rs.ActiveConnection = m_conn
131
m_rs.Open "SELECT * FROM Users WHERE userid = '" &
132
Request.ServerVariables("LOGON_USER") & "'"
133
If Not m_rs.EOF Then
134
m_username = m_rs.Fields("username")
135
m_userphone = m_rs.Fields("userphone")
136
End If
137
End Sub
138![](/Images/OutliningIndicators/None.gif)
139
Private Sub Class_Terminate()
140
On Error Resume Next
141
m_rs.Close
142
Set m_rs = Nothing
143
m_conn.Close
144
Set m_conn = Nothing
145
End Sub
146![](/Images/OutliningIndicators/None.gif)
147
End Class
148![](/Images/OutliningIndicators/None.gif)
149
从例子中你可以看到,UserName 和 UserPhone 属性在类初始化之后就会有值。你可以将这个类的代码放到 ASP 代码的最后。然后在其余的代码中其功能就可以在使用,不用实例化ADO对象,进行数据库调用和之后的清理。而且,如果你使用的是 Microsoft Visual InterDev,那么类中定义的属性和方法可以通过智能感知(IntelliSense)使用。
150![](/Images/OutliningIndicators/None.gif)
151
智能感知只对一级属性和方法有效,所以如果你创建一个类返回其它 ASP 类的对象,那么 InterDev 就不会提供可用的方法和属性。
152![](/Images/OutliningIndicators/None.gif)
153
下面的代码是一个类的骨架:
154![](/Images/OutliningIndicators/None.gif)
155
Class MyClass
156![](/Images/OutliningIndicators/None.gif)
157
Private Sub Class_Initialize()
158
End Sub
159![](/Images/OutliningIndicators/None.gif)
160
Private Sub Class_Terminate()
161
End Sub
162![](/Images/OutliningIndicators/None.gif)
163
End Class
164![](/Images/OutliningIndicators/None.gif)
165
使用这个基本信息,一个类被声明,并且可以使用它来创建对象。但是这个类的对象没有功能——它们是无用的东西。类的方法从 Public Sub 或者 Public Function 声明构建(Subs 没有返回值,而 Function 有返回值)。
166![](/Images/OutliningIndicators/None.gif)
167
有两个方法可以暴露属性:既可以在类的全局上下文中使用 Public VarName,也可以使用 Public Property Get/Set/Let。如果你需要一个只读属性,那么只要简单地使用一个 Property Get 声明即可。所有其它声明为 Private 的 Properties,Subs 或者 Functions 只有在实例化的对象才可用。在类的全局上下文中声明为 Private 的变量是成员变量。
168![](/Images/OutliningIndicators/None.gif)
169
创建类的另外一个好处是你可以轻易的将这个类声明移值到 Web 服务中。因为这是很多开发人员前进的方向,你可以通过使用类创建面向对象的代码先行一步。当你移值这些类声明到一个 Web 服务时,你不需要重写所有的放置了那段代码的ASP代码。所要做的唯一的改变就是你的对象的实例化。
170![](/Images/OutliningIndicators/None.gif)
171
我发现创建这个抽象的最大的优点是可以使用其它 Web 开发者的帮助。因为对于很多开发人员来说在每个不同的实现中重复创建相同的代码效率相当低,所以这个方法为每个开发人员提供了一个抽象工具。这就允许每个开发人员将精力更多地集中在当前工作的功能上面,而不是花费时间重写他们已经开发过的代码上面。
172![](/Images/OutliningIndicators/None.gif)
173
如果你奇怪为什么我不提供一个COM对象来处理这个功能,那是因为它会把你锁定到细节设计中。通过在 ASP 中动态地创建类,你可以不用重新编译和发布就能够对类做出更改。在类固定之后,再将它移值到 COM、Windows 脚本组件(WSC)或者一个 Web 服务中,但是在开发阶段还是灵活一些好。
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/None.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/None.gif)
18
![](/Images/OutliningIndicators/None.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/None.gif)
22
![](/Images/OutliningIndicators/None.gif)
23
![](/Images/OutliningIndicators/None.gif)
24
![](/Images/OutliningIndicators/None.gif)
25
![](/Images/OutliningIndicators/None.gif)
26
![](/Images/OutliningIndicators/None.gif)
27
![](/Images/OutliningIndicators/None.gif)
28
![](/Images/OutliningIndicators/None.gif)
29
![](/Images/OutliningIndicators/None.gif)
30
![](/Images/OutliningIndicators/None.gif)
31
![](/Images/OutliningIndicators/None.gif)
32
![](/Images/OutliningIndicators/None.gif)
33
![](/Images/OutliningIndicators/None.gif)
34
![](/Images/OutliningIndicators/None.gif)
35
![](/Images/OutliningIndicators/None.gif)
36
![](/Images/OutliningIndicators/None.gif)
37
![](/Images/OutliningIndicators/None.gif)
38
![](/Images/OutliningIndicators/None.gif)
39
![](/Images/OutliningIndicators/None.gif)
40
![](/Images/OutliningIndicators/None.gif)
41
![](/Images/OutliningIndicators/None.gif)
42
![](/Images/OutliningIndicators/None.gif)
43
![](/Images/OutliningIndicators/None.gif)
44
![](/Images/OutliningIndicators/None.gif)
45
![](/Images/OutliningIndicators/None.gif)
46
![](/Images/OutliningIndicators/None.gif)
47
![](/Images/OutliningIndicators/None.gif)
48
![](/Images/OutliningIndicators/None.gif)
49
![](/Images/OutliningIndicators/None.gif)
50
![](/Images/OutliningIndicators/None.gif)
51
![](/Images/OutliningIndicators/None.gif)
52
![](/Images/OutliningIndicators/None.gif)
53
![](/Images/OutliningIndicators/None.gif)
54
![](/Images/OutliningIndicators/None.gif)
55
![](/Images/OutliningIndicators/None.gif)
56
![](/Images/OutliningIndicators/None.gif)
57
![](/Images/OutliningIndicators/None.gif)
58
![](/Images/OutliningIndicators/None.gif)
59
![](/Images/OutliningIndicators/None.gif)
60
![](/Images/OutliningIndicators/None.gif)
61
![](/Images/OutliningIndicators/None.gif)
62
![](/Images/OutliningIndicators/None.gif)
63
![](/Images/OutliningIndicators/None.gif)
64
![](/Images/OutliningIndicators/None.gif)
65
![](/Images/OutliningIndicators/None.gif)
66
![](/Images/OutliningIndicators/None.gif)
67
![](/Images/OutliningIndicators/None.gif)
68
![](/Images/OutliningIndicators/None.gif)
69
![](/Images/OutliningIndicators/None.gif)
70
![](/Images/OutliningIndicators/None.gif)
71
![](/Images/OutliningIndicators/None.gif)
72
![](/Images/OutliningIndicators/None.gif)
73
![](/Images/OutliningIndicators/None.gif)
74
![](/Images/OutliningIndicators/None.gif)
75
![](/Images/OutliningIndicators/None.gif)
76
![](/Images/OutliningIndicators/None.gif)
77
![](/Images/OutliningIndicators/None.gif)
78
![](/Images/OutliningIndicators/None.gif)
79
![](/Images/OutliningIndicators/None.gif)
80
![](/Images/OutliningIndicators/None.gif)
81
![](/Images/OutliningIndicators/None.gif)
82
![](/Images/OutliningIndicators/None.gif)
83
![](/Images/OutliningIndicators/None.gif)
84
![](/Images/OutliningIndicators/None.gif)
85
![](/Images/OutliningIndicators/None.gif)
86
![](/Images/OutliningIndicators/None.gif)
87
![](/Images/OutliningIndicators/None.gif)
88
![](/Images/OutliningIndicators/None.gif)
89
![](/Images/OutliningIndicators/None.gif)
90
![](/Images/OutliningIndicators/None.gif)
91
![](/Images/OutliningIndicators/None.gif)
92
![](/Images/OutliningIndicators/None.gif)
93
![](/Images/OutliningIndicators/None.gif)
94
![](/Images/OutliningIndicators/None.gif)
95
![](/Images/OutliningIndicators/None.gif)
96
![](/Images/OutliningIndicators/None.gif)
97
![](/Images/OutliningIndicators/None.gif)
98
![](/Images/OutliningIndicators/None.gif)
99
![](/Images/OutliningIndicators/None.gif)
100
![](/Images/OutliningIndicators/None.gif)
101
![](/Images/OutliningIndicators/None.gif)
102
![](/Images/OutliningIndicators/None.gif)
103
![](/Images/OutliningIndicators/None.gif)
104
![](/Images/OutliningIndicators/None.gif)
105
![](/Images/OutliningIndicators/None.gif)
106
![](/Images/OutliningIndicators/None.gif)
107
![](/Images/OutliningIndicators/None.gif)
108
![](/Images/OutliningIndicators/None.gif)
109
![](/Images/OutliningIndicators/None.gif)
110
![](/Images/OutliningIndicators/None.gif)
111
![](/Images/OutliningIndicators/None.gif)
112
![](/Images/OutliningIndicators/None.gif)
113
![](/Images/OutliningIndicators/None.gif)
114
![](/Images/OutliningIndicators/None.gif)
115
![](/Images/OutliningIndicators/None.gif)
116
![](/Images/OutliningIndicators/None.gif)
117
![](/Images/OutliningIndicators/None.gif)
118
![](/Images/OutliningIndicators/None.gif)
119
![](/Images/OutliningIndicators/None.gif)
120
![](/Images/OutliningIndicators/None.gif)
121
![](/Images/OutliningIndicators/None.gif)
122
![](/Images/OutliningIndicators/None.gif)
123
![](/Images/OutliningIndicators/None.gif)
124
![](/Images/OutliningIndicators/None.gif)
125
![](/Images/OutliningIndicators/None.gif)
126
![](/Images/OutliningIndicators/None.gif)
127
![](/Images/OutliningIndicators/None.gif)
128
![](/Images/OutliningIndicators/None.gif)
129
![](/Images/OutliningIndicators/None.gif)
130
![](/Images/OutliningIndicators/None.gif)
131
![](/Images/OutliningIndicators/None.gif)
132
![](/Images/OutliningIndicators/None.gif)
133
![](/Images/OutliningIndicators/None.gif)
134
![](/Images/OutliningIndicators/None.gif)
135
![](/Images/OutliningIndicators/None.gif)
136
![](/Images/OutliningIndicators/None.gif)
137
![](/Images/OutliningIndicators/None.gif)
138
![](/Images/OutliningIndicators/None.gif)
139
![](/Images/OutliningIndicators/None.gif)
140
![](/Images/OutliningIndicators/None.gif)
141
![](/Images/OutliningIndicators/None.gif)
142
![](/Images/OutliningIndicators/None.gif)
143
![](/Images/OutliningIndicators/None.gif)
144
![](/Images/OutliningIndicators/None.gif)
145
![](/Images/OutliningIndicators/None.gif)
146
![](/Images/OutliningIndicators/None.gif)
147
![](/Images/OutliningIndicators/None.gif)
148
![](/Images/OutliningIndicators/None.gif)
149
![](/Images/OutliningIndicators/None.gif)
150
![](/Images/OutliningIndicators/None.gif)
151
![](/Images/OutliningIndicators/None.gif)
152
![](/Images/OutliningIndicators/None.gif)
153
![](/Images/OutliningIndicators/None.gif)
154
![](/Images/OutliningIndicators/None.gif)
155
![](/Images/OutliningIndicators/None.gif)
156
![](/Images/OutliningIndicators/None.gif)
157
![](/Images/OutliningIndicators/None.gif)
158
![](/Images/OutliningIndicators/None.gif)
159
![](/Images/OutliningIndicators/None.gif)
160
![](/Images/OutliningIndicators/None.gif)
161
![](/Images/OutliningIndicators/None.gif)
162
![](/Images/OutliningIndicators/None.gif)
163
![](/Images/OutliningIndicators/None.gif)
164
![](/Images/OutliningIndicators/None.gif)
165
![](/Images/OutliningIndicators/None.gif)
166
![](/Images/OutliningIndicators/None.gif)
167
![](/Images/OutliningIndicators/None.gif)
168
![](/Images/OutliningIndicators/None.gif)
169
![](/Images/OutliningIndicators/None.gif)
170
![](/Images/OutliningIndicators/None.gif)
171
![](/Images/OutliningIndicators/None.gif)
172
![](/Images/OutliningIndicators/None.gif)
173
![](/Images/OutliningIndicators/None.gif)
posted on 2005-06-05 22:19 ξσ Dicky σξ 阅读(956) 评论(0) 编辑 收藏 举报