Keys 메소드

Dictionary 객체의 모든 기존 키를 포함하는 배열을 반환합니다.

구문

object.Keys( )

인수

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

주석

다음 코드는 Keys 메소드의 사용을 보여줍니다.

예 1:

Function DicDemo
    Dim a, d, i, s   ' 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"

    ' Get the keys.
    a = d.Keys

    ' Iterate the array
    For i = 0 To d.Count - 1
        s = s & a(i) ' Create return string
    Next

    DicDemo = s
End Function

' Usage
Dim result
result = DicDemo
' Output: a b c

예 2:

Function DicKeysWithItems
    Dim a, d, i, s   ' Create some variables.
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "x", "Xenon"
    d.Add "y", "Yttrium"
    d.Add "z", "Zirconium"

    ' Get the keys.
    a = d.Keys

    ' Iterate the array
    For i = 0 To d.Count - 1
        s = s & "Key: " & a(i) & ", Item: " & d.Item(a(i))
    Next

    DicKeysWithItems = s
End Function

' Usage
Dim result
result = DicKeysWithItems
' Output: Key: x, Item: Xenon Key: y, Item: Yttrium Key: z, Item: Zirconium

예 3:

Function CheckAndRemoveKey(keyToRemove)
    Dim a, d, i, s   ' Create some variables.
    Set d = CreateObject("Scripting.Dictionary")

    ' Add some keys and items.
    d.Add "m", "Moscow"
    d.Add "t", "Tokyo"
    d.Add "n", "New York"

    ' Check and remove key
    If d.Exists(keyToRemove) Then
        d.Remove(keyToRemove)
        s = "Key '" & keyToRemove & "' removed."
    Else
        s = "Key '" & keyToRemove & "' not found."
    End If

    ' Get the remaining keys.
    a = d.Keys
    For i = 0 To d.Count - 1
        s = s & "Remaining Key: " & a(i)
    Next

    CheckAndRemoveKey = s
End Function

' Usage
Dim result
result = CheckAndRemoveKey("t")
' Output: Key 't' removed. Remaining Key: m Remaining Key: n