在 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