屬性和方法

一個指令碼 Dictionary 物件中鍵/專案對儲存資訊。Keys 必須是唯一的而不是陣列,但關聯的 Items 可以重複(它們的唯一性由伴侶 Key 儲存),並且可以是任何型別的變體或物件。

字典可以被認為是兩個欄位的記憶體資料庫,在第一個欄位Key ) 上具有主要的唯一索引。Keys 屬性上的這個唯一索引允許非常快速的查詢來檢索 Key 的關聯 Item 值。

屬性

名稱 讀/寫 型別 描述
CompareMode 讀/寫 CompareMode 常量 設定 CompareMode 只能在空字典上執行。可接受的值為 0(vbBinaryCompare),1(vbTextCompare),2(vbDatabaseCompare)
計數 只讀 無符號長整數 指令碼字典物件中的鍵/項對的基於一的計數。
讀/寫 非陣列變體 字典中的每個單獨的唯一鍵。
物品( 讀/寫 任何變種 預設屬性。每個單獨的專案與字典中的鍵相關聯。請注意,嘗試使用字典中不存在的鍵檢索專案將隱式新增傳遞的鍵。

方法

名稱 描述
新增( 專案 將新的 Key 和 Item 新增到字典中。新金鑰不得存在於字典的當前 Keys 集合中,但可以在許多唯一鍵中重複一個專案。
存在( 布林測試,用於確定字典中是否已存在金鑰。
按鍵 返回唯一鍵的陣列或集合。
專案 返回關聯項的陣列或集合。
刪除( 刪除單個字典鍵及其關聯項。
移除所有 清除所有字典物件的鍵和項。

示例程式碼

'Populate, enumerate, locate and remove entries in a dictionary that was created
'with late binding
Sub iterateDictionaryLate()
    Dim k As Variant, dict As Object
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare          'non-case sensitive compare model
    
    'populate the dictionary
    dict.Add Key:="Red", Item:="Balloon"
    dict.Add Key:="Green", Item:="Balloon"
    dict.Add Key:="Blue", Item:="Balloon"
    
    'iterate through the keys
    For Each k In dict.Keys
        Debug.Print k & " - " & dict.Item(k)
    Next k

    'locate the Item for Green
    Debug.Print dict.Item("Green")
    
    'remove key/item pairs from the dictionary
    dict.Remove "blue"      'remove individual key/item pair by key
    dict.RemoveAll          'remove all remaining key/item pairs

End Sub

'Populate, enumerate, locate and remove entries in a dictionary that was created
'with early binding (see Remarks)
Sub iterateDictionaryEarly()
    Dim d As Long, k As Variant
    Dim dict As New Scripting.Dictionary
    
    dict.CompareMode = vbTextCompare          'non-case sensitive compare model
    
    'populate the dictionary
    dict.Add Key:="Red", Item:="Balloon"
    dict.Add Key:="Green", Item:="Balloon"
    dict.Add Key:="Blue", Item:="Balloon"
    dict.Add Key:="White", Item:="Balloon"
    
    'iterate through the keys
    For Each k In dict.Keys
        Debug.Print k & " - " & dict.Item(k)
    Next k

    'iterate through the keys by the count
    For d = 0 To dict.Count - 1
        Debug.Print dict.Keys(d) & " - " & dict.Items(d)
    Next d
    
    'iterate through the keys by the boundaries of the keys collection
    For d = LBound(dict.Keys) To UBound(dict.Keys)
        Debug.Print dict.Keys(d) & " - " & dict.Items(d)
    Next d
    
    'locate the Item for Green
    Debug.Print dict.Item("Green")
    'locate the Item for the first key
    Debug.Print dict.Item(dict.Keys(0))
    'locate the Item for the last key
    Debug.Print dict.Item(dict.Keys(UBound(dict.Keys)))
    
    'remove key/item pairs from the dictionary
    dict.Remove "blue"                         'remove individual key/item pair by key
    dict.Remove dict.Keys(0)                   'remove first key/item by index position
    dict.Remove dict.Keys(UBound(dict.Keys))   'remove last key/item by index position
    dict.RemoveAll                             'remove all remaining key/item pairs

End Sub