「VBScript」HashtableクラスのContainsKey()とContainsValue()でキーと値の存在を判定する
書式
ContainsKey(Object)
Hashtable に特定のキーが格納されているかどうかを判断します。
ContainsValue(Object)
Hashtable に特定の値が格納されているかどうかを判断します。
使用例
Dim cftHash
Set cftHash = CreateObject("System.Collections.Hashtable")
' 要素を追加
cftHash.Add "city01", "testvalue:001"
cftHash.Add "city02", "testvalue:002"
cftHash.Add "city03", "testvlaue:003"
cftHash.Add "city04", "testvlaue:004"
' 追加要素の参照
Wscript.echo cftHash("city01")
Wscript.echo cftHash("city02")
Wscript.echo cftHash("city03")
Wscript.echo cftHash("city04")
' キーが存在判定
If cftHash.ContainsKey("city04") Then
Wscript.echo "city04 exists"
End If
' 値が存在判定
If cftHash.ContainsValue("testvlaue:003") Then
Wscript.echo "value exists"
End If
' 要素を削除
cftHash.Clear
Dim cftHash
Set cftHash = CreateObject("System.Collections.Hashtable")
' 要素を追加
cftHash.Add "city01", "testvalue:001"
cftHash.Add "city02", "testvalue:002"
cftHash.Add "city03", "testvlaue:003"
cftHash.Add "city04", "testvlaue:004"
' 追加要素の参照
Wscript.echo cftHash("city01")
Wscript.echo cftHash("city02")
Wscript.echo cftHash("city03")
Wscript.echo cftHash("city04")
' キーが存在判定
If cftHash.ContainsKey("city04") Then
Wscript.echo "city04 exists"
End If
' 値が存在判定
If cftHash.ContainsValue("testvlaue:003") Then
Wscript.echo "value exists"
End If
' 要素を削除
cftHash.Clear
Dim cftHash Set cftHash = CreateObject("System.Collections.Hashtable") ' 要素を追加 cftHash.Add "city01", "testvalue:001" cftHash.Add "city02", "testvalue:002" cftHash.Add "city03", "testvlaue:003" cftHash.Add "city04", "testvlaue:004" ' 追加要素の参照 Wscript.echo cftHash("city01") Wscript.echo cftHash("city02") Wscript.echo cftHash("city03") Wscript.echo cftHash("city04") ' キーが存在判定 If cftHash.ContainsKey("city04") Then Wscript.echo "city04 exists" End If ' 値が存在判定 If cftHash.ContainsValue("testvlaue:003") Then Wscript.echo "value exists" End If ' 要素を削除 cftHash.Clear
実行結果
testvalue:001
testvalue:002
testvalue:003
testvalue:004
city04 exists
value exists