如何检查字典是否包含给定值?

Ama*_*mal 3 vb.net

如何检查某个值是否存在Dictionary(Of int, String)

让我说我有[{1, 'One'};{2, 'Two'};{3, 'Three'}],如何检查'两个'是否存在?

rae*_*aed 14

你可以使用ContainsValue:

If myDictionary.ContainsValue("Two") Then
    debug.print("Exists")
End If
Run Code Online (Sandbox Code Playgroud)

这就是你所需要的.


cyb*_*onk 5

补充 raed\xc2\xb4s 答案,您还可以使用ContainsKey来搜索键而不是值。

\n\n
If myDictionary.ContainsKey(1) Then\n    debug.print("Exists")\nEnd If\n
Run Code Online (Sandbox Code Playgroud)\n\n

这也适用于字符串键,如示例所示:

\n\n
[{"Chris", "Alive"};{"John", "Deceased"}]\n\nIf myDictionary.ContainsKey("Chris") Then\n    debug.print("Chris Exists in dictionary")\nEnd If\n\nIf myDictionary.ContainsValue("Alive") Then\n    debug.print("There is someone alive in the dictionary")\nEnd If\n
Run Code Online (Sandbox Code Playgroud)\n