属性和方法

一个脚本 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