VBScript 字典对象
Dictionary 对象可以与 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 对象关联的方法,使开发人员能够无缝地使用字典对象。
Exist 方法
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 Method
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 Method
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
Remove All Method
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.html