Categories: VBScript 教程

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

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

5 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago