指定されたキーが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