VBScript高级: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对象相关联的各种方法使开发人员能够无缝处理字典对象。

Exists方法

Exists方法可帮助用户检查键值对是否存在。

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

RemoveAl方法

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>

作者:terry,如若转载,请注明出处:https://www.web176.com/vbscript/1123.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年12月7日 下午12:30
下一篇 2020年12月7日 下午12:36

相关推荐

发表回复

登录后才能评论