O'Reilly logo

VBScript in a Nutshell, 2nd Edition by Ron Petrusha, Matt Childs, Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required