Metodo Key

Imposta una chiave in un oggetto Dictionary.

Sintassi

object.Key(key) = newkey

Argomenti:

  • Object: obbligatorio. Sempre il nome dell'oggetto Dictionary.

  • Key: obbligatorio. Valore della chiave in fase di modifica.

  • Newkey: obbligatorio. Nuovo valore che sostituisce la chiave specificata.

Note

Se l'argomento key non viene trovato quando si modifica una chiave, viene creata una nuova chiave e l'elemento associato viene lasciato vuoto.

Nell'esempio seguente viene illustrato l'uso della proprietà Key.

Esempio 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

Esempio 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