Name

Dictionary.Key Property

Syntax

                  dictionaryobject.Key(key) = newkey
dictionaryobject

Use: Required

Data Type: Dictionary object

A reference to a Dictionary object.

key

Use: Required

Data Type: String

The key of an existing dictionary item.

newkey

Use: 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.

  • newkey must be unique and must not already exist in the dictionary or an error results.

  • The comparison of key and newkey 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 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 ...

Get VBScript in a Nutshell, 2nd Edition 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.