Key 메소드

Dictionary 객체의 키를 설정합니다.

구문

object.Key(key) = newkey

인수:

  • Object: 필수. 항상 Dictionary 객체의 이름입니다.

  • Key: 필수. 변경 중인 키 값입니다.

  • Newkey: 필수. 지정된 키를 대체하는 새 값입니다.

주석

키를 변경할 때 키를 찾을 수 없는 경우 새 키가 생성되고 연계된 항목이 빈 상태를 유지합니다.

다음 예에서는 Key 등록정보의 사용을 보여 줍니다.

예 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

예 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