- VBScript 教程
- VBScript - 首页
- VBScript - 概述
- VBScript - 语法
- VBScript - 启用
- VBScript - 放置
- VBScript - 变量
- VBScript - 常量
- VBScript - 运算符
- VBScript - 决策
- VBScript - 循环
- VBScript - 事件
- VBScript - Cookie
- VBScript - 数字
- VBScript - 字符串
- VBScript - 数组
- VBScript - 日期
- VBScript 高级
- VBScript - 过程
- VBScript - 对话框
- VBScript - 面向对象
- VBScript - 正则表达式
- VBScript - 错误处理
- VBScript - 其他语句
- VBScript 有用资源
- VBScript - 问答
- VBScript - 快速指南
- VBScript - 有用资源
- VBScript - 讨论
VBScript 字典对象
字典对象可以比作 PERL 关联数组。任何值都可以存储在数组中,并且每个项目都与一个唯一的键相关联。键用于检索单个元素,它通常是整数或字符串,但可以是除数组之外的任何内容。
语法
VBScript 类包含在Class .... End Class之间。
Dim variablename
Set variablename = CreateObject("Scripting.Dictionary")
variablename.Add (key, item)
示例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
Dim obj_datadict ' Create a variable.
Set obj_datadict = CreateObject("Scripting.Dictionary")
obj_datadict.Add "a", "Apple" ' Add some keys and items.
obj_datadict.Add "b", "Bluetooth"
obj_datadict.Add "c", "Clear"
</script>
</body>
</html>
与 DataDictionary 对象相关联的各种方法使开发人员能够无缝地使用字典对象。
Exists 方法
Exist 方法帮助用户检查键值对是否存在。
object.Exists(key)
参数说明
Object,必填参数。这表示字典对象的名称。
Key,必填参数。这表示字典对象的值。
示例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
Dim d, msg ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Apple" ' Add some keys and items.
d.Add "b", "BlueTooth"
d.Add "c", "C++"
If d.Exists("c") Then
msgbox "Specified key exists."
Else
msgbox "Specified key doesn't exist."
End If
</script>
</body>
</html>
将文件另存为 .HTML,并在 IE 中执行上述脚本后,它会在消息框中显示以下消息。
Specified key exists.
Items 方法
Items 方法帮助我们获取存储在数据字典对象键值对中的值。
object.Items( )
参数说明
Object,必填参数。这表示字典对象的名称。
示例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
Dim obj_datadict ' Create a variable.
Set obj_datadict = CreateObject("Scripting.Dictionary")
obj_datadict.Add "a", "Apple" ' Add some keys and items.
obj_datadict.Add "b", "Bluetooth"
obj_datadict.Add "c", "C++"
a = obj_datadict.items
msgbox a(0)
msgbox a(2)
</script>
</body>
</html>
将文件另存为 .HTML,并在 IE 中执行上述脚本后,它会在消息框中显示以下消息。
Apple C++
Keys 方法
object.Keys( )
参数说明
Object,必填参数。这表示字典对象的名称。
示例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
Dim obj_datadict ' Create a variable.
Set obj_datadict = CreateObject("Scripting.Dictionary")
obj_datadict.Add "a", "Apple" ' Add some keys and items.
obj_datadict.Add "b", "Bluetooth"
obj_datadict.Add "c", "C++"
a = obj_datadict.Keys
msgbox a(0)
msgbox a(2)
</script>
</body>
</html>
将文件另存为 .HTML,并在 IE 中执行上述脚本后,它会在消息框中显示以下消息。
a c
Remove 方法
object.Remove(key)
参数说明
Object,必填参数。这表示字典对象的名称。
Key,必填参数。这表示需要从字典对象中删除的键值对。
示例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
Dim obj_datadict ' Create a variable.
Set obj_datadict = CreateObject("Scripting.Dictionary")
obj_datadict.Add "a", "Apple" ' Add some keys and items.
obj_datadict.Add "b", "Bluetooth"
obj_datadict.Add "c", "C++"
a = obj_datadict.Keys
msgbox a(0)
msgbox a(2)
obj_datadict.remove("b") 'The key value pair of "b" is removed'
</script>
</body>
</html>
将文件另存为 .HTML,并在 IE 中执行上述脚本后,它会在消息框中显示以下消息。
a c
RemoveAll 方法
object.RemoveAll()
参数说明
Object,必填参数。这表示字典对象的名称。
示例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
Dim obj_datadict ' Create a variable.
Set obj_datadict = CreateObject("Scripting.Dictionary")
obj_datadict.Add "a", "Apple" ' Add some keys and items.
obj_datadict.Add "b", "Bluetooth"
obj_datadict.Add "c", "C++"
a = obj_datadict.Keys
msgbox a(0)
msgbox a(2)
obj_datadict.removeall
</script>
</body>
</html>
vbscript_object_oriented.htm
广告