在 Dictionary 对象中设置键。
语法
object.Key(key) = newkey
参数:
Object:必需。始终为 Dictionary 对象的名称。
Key:必需。要更改的键值。
Newkey:必需。用于替换指定键的新值。
注释
如果更改键时未找到键,则创建新键,并将其关联项留空。
以下示例说明了 Key 属性的用法:
示例 1:
Function ChangeKey
Dim d ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
' Change the key "c" to "d"
d.Key("c") = "d"
' Return the associated item for the new key "d"
ChangeKey = d.Item("d")
End Function
' Usage
Dim result
result = ChangeKey()
' Output: Cairo
示例 2:
Function UpdateKeysAndDisplay
Dim d, s ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
' Add some keys and items.
d.Add "m", "Moscow"
d.Add "n", "New York"
d.Add "p", "Paris"
' Update keys
d.Key("m") = "r" ' Change "m" to "r"
d.Key("n") = "s" ' Change "n" to "s"
' Display all items with updated keys
s = ""
For Each Key In d.Keys
s = s & "Key: " & Key & ", Item: " & d.Item(Key)
Next
UpdateKeysAndDisplay = s
End Function
' Usage
Dim result
result = UpdateKeysAndDisplay()
' Output: Key: r, Item: Moscow Key: s, Item: New York Key: p, Item: Paris