Exists 메소드

지정된 키가 Dictionary 객체에 존재할 경우 true를 반환하고, 존재하지 않을 경우 false를 반환합니다.

구문

object.Exists(key)

인수:

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

  • Key: 필수. Dictionary 객체에서 검색 중인 키 값입니다.

주석

다음 예에서는 Exists 메소드의 사용을 보여 줍니다.

예 1:

Function KeyExistsDemo
    Dim d, msg   ' 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"

    ' Check if key exists
    If d.Exists("c") Then
        msg = "Specified key exists."
    Else
        msg = "Specified key doesn't exist."
    End If

    KeyExistsDemo = msg
End Function

' Usage
Dim result
result = KeyExistsDemo
'Output: Specified key exists.

예 2:

Function CheckMultipleKeys
    Dim d, msg   ' 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"

    ' Check if keys exist
    msg = ""
    keysToCheck = Array("x", "y", "w")

    For Each key In keysToCheck
        If d.Exists(key) Then
            msg = msg & "Key '" & key & "' exists."
        Else
            msg = msg & "Key '" & key & "' doesn't exist."
        End If
    Next

    CheckMultipleKeys = msg
End Function

' Usage
Dim result
result = CheckMultipleKeys
'Output: Key 'x' exists.Key 'y' exists.Key 'w' doesn't exist.

예 3:

Function AddKeysIfNotExist
    Dim d, msg   ' Create some variables.
    Set d = CreateObject("Scripting.Dictionary")

    ' Add initial keys and items.
    d.Add "m", "Moscow"
    d.Add "t", "Tokyo"

    ' Check and add keys
    keysToAdd = Array("m", "s", "t")

    For Each key In keysToAdd
        If Not d.Exists(key) Then
            d.Add key, "New City"
        End If
    Next

    ' Display all keys and items
    msg = ""
    For Each key In d.Keys
        msg = msg & "Key: " & key & ", Item: " & d.Item(key)
    Next

    AddKeysIfNotExist = msg
End Function

' Usage
Dim result
result = AddKeysIfNotExist
'Output: Key: m, Item: Moscow Key: t, Item: Tokyo Key: s, Item: New City