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.