Name
Dictionary.Key Property
Syntax
dictionaryobject.Key(key) =newkey
dictionaryobjectUse: Required
Data Type: Dictionary object
A reference to a Dictionary object.
keyUse: Required
Data Type: String
The key of an existing dictionary item.
newkeyUse: Required
Data Type: String
A new unique key for this dictionary item.
Data Type
A String.
Description
Replaces an existing key with a new one.
Rules at a Glance
The Key property is write-only.
key, the existing key value, must exist in the dictionary or an error results.newkeymust be unique and must not already exist in the dictionary or an error results.The comparison of
keyandnewkeywith existing key values is defined by the Dictionary object’s CompareMode property.
Example
Private Function ChangeKeyValue(sOldKey, sNewKey)
'Assumes oDictionary is a public object
If oDictionary.Exists(sOldKey) Then
oDictionary.Key(sOldKey) = sNewKey
ChangeKeyValue = True
Else
ChangeKeyValue = False
End If
End FunctionProgramming Tips and Gotchas
Use the Key property to change the name of an existing key. Use the Add method to add a new key and its associated value to the Dictionary object. Use the Keys method to retrieve the names of all keys; this is especially useful when you don’t know the names or the contents of the dictionary in advance.
Attempting to retrieve the key name (a nonsensical operation, since this amounts to providing the key’s name in order to retrieve the key’s name) generates an error, as does attempting to modify a key name that hasn’t already ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access