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
广告

© . All rights reserved.