「VB.NET」Dictionaryに指定の値があるか真偽値を返す方法

2021年9月14日

書式
Public Function ContainsValue (value As TValue) As Boolean
指定した値を持つ要素が true に格納されている場合は Dictionary<TKey,TValue>。それ以外の場合は false。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Module Module1
Sub Main()
'Dictionary型変数の定義
Dim dct As New Dictionary(Of String, Integer)
'キーと値を追加
dct.Add("key1", 111)
dct.Add("key2", 222)
dct.Add("key3", 333)
dct.Add("key4", 444)
'指定値444の存在判定
If (dct.ContainsValue("444")) Then
Console.WriteLine("指定値444が存在します")
End If
'指定値111の存在判定
If (dct.ContainsValue("111")) Then
Console.WriteLine("指定値111が存在します")
End If
Console.ReadKey()
End Sub
End Module
Module Module1 Sub Main() 'Dictionary型変数の定義 Dim dct As New Dictionary(Of String, Integer) 'キーと値を追加 dct.Add("key1", 111) dct.Add("key2", 222) dct.Add("key3", 333) dct.Add("key4", 444) '指定値444の存在判定 If (dct.ContainsValue("444")) Then Console.WriteLine("指定値444が存在します") End If '指定値111の存在判定 If (dct.ContainsValue("111")) Then Console.WriteLine("指定値111が存在します") End If Console.ReadKey() End Sub End Module
Module Module1
    Sub Main()

        'Dictionary型変数の定義
        Dim dct As New Dictionary(Of String, Integer)
        'キーと値を追加
        dct.Add("key1", 111)
        dct.Add("key2", 222)
        dct.Add("key3", 333)
        dct.Add("key4", 444)

        '指定値444の存在判定
        If (dct.ContainsValue("444")) Then
            Console.WriteLine("指定値444が存在します")
        End If

        '指定値111の存在判定
        If (dct.ContainsValue("111")) Then
            Console.WriteLine("指定値111が存在します")
        End If

        Console.ReadKey()

    End Sub

End Module

実行結果
指定値444が存在します
指定値111が存在します

VB.net

Posted by arkgame