Name
Dictionary.Key Property
Data Subtype
String
Syntax
dictionaryobject
.Key(key
) =newkey
-
dictionaryobject
Use: Required
Data Subtype: Dictionary object
A reference to a Dictionary object.
-
key
Use: Required
Data Subtype: String
The key of an existing dictionary item.
-
newkey
Use: Required
Data Subtype: String
A new unique key for this dictionary item.
Property Datatype
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.newkey
must be unique and must not already exist in the dictionary or an error results.The comparison of
key
andnewkey
with 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 Function
Programming Tips & 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 ...
Get VBScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.