无法在字典中搜索

我找到了一些将变量保存为字典类型的代码,但是无法执行搜索。我尝试了不同的事情,并得到了不同的错误。我的变量是:

Public controlList As New List(Of Dictionary(Of String, String))()

如何在此词典中搜索项目?

编辑:我在名为GlobalVariables的模块中创建了此变量,并试图从ControlAdderClass类中调用它

和我尝试的代码之一:

For i As Integer = 0 To controlList.Count - 1
    Dim value As Dictionary(Of String, String) = controlList(i)
    Try
        MessageBox.Show(controlList(i), Str(value))
    Catch
        Console.WriteLine("Error")
    End Try
Next

这多次给我这个错误:

类型“ System.InvalidCastException”的第一次机会异常发生在mirc_dialog.exe中
比昂·罗格·克林佐(Bjørn-RogerKringsjå)

问题

您应该做的第一件事是将选项设置为严格完成后,VS会出错以下行:

MessageBox.Show(controlList(i), Str(value))

出现以下错误消息:

重载解析失败,因为无法使用这些参数调用可访问的“显示”。

messagebox类没有Show接受Dictionary(Of String, String)like方法的重载

Public Shared Function Show(text As Dictionary(Of String, String), caption As String) As DialogResult

如果不严格选择选项,VS将选择与参数数量匹配的第一个重载。在您的情况下,这是重载

Public Shared Function Show(owner As IWin32Window, text As String ) As DialogResult

并且观察到,您不能将字典强制转换为IWin32Window。


解决方案

“如何在此词典中搜索项目?”

好吧,这是一个简单的例子:

Dim keyToFind As String = "a_key"
Dim valueToFind As String = "some_value"

For Each dictionary As Dictionary(Of String, String) In GlobalVariables.ControlList
    For Each pair As KeyValuePair(Of String, String) In dictionary

        Dim keyMatched As Boolean = (String.Compare(pair.Key, keyToFind, False) = 0)
        Dim valueMatched As Boolean = (String.Compare(pair.Value, valueToFind, False) = 0)

        If (keyMatched AndAlso valueMatched) Then
            MessageBox.Show(String.Format("Key={0}, Value={1}", pair.Key, pair.Value), "Found")
        ElseIf (keyMatched) Then
            MessageBox.Show(String.Format("Key={0}", pair.Key), "Found")
        ElseIf (valueMatched) Then
            MessageBox.Show(String.Format("Value={1}", pair.Value), "Found")
        End If

    Next
Next

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章