Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在字典中搜索字符串_C#_String_Linq_Dictionary - Fatal编程技术网

C# 在字典中搜索字符串

C# 在字典中搜索字符串,c#,string,linq,dictionary,C#,String,Linq,Dictionary,我在Form1.cs类中创建了GUI,它还有一个具有以下功能的组合框: string SelectedItemName = (string)comboBox2.SelectedItem.ToString(); Console.WriteLine(SelectedItemName); if (comboBox2.SelectedIndex > -1) { testvariabel2.GetSessionName(); } 因此,我检查用户是否从ComboBox中选择了某些内容,然后

我在Form1.cs类中创建了GUI,它还有一个具有以下功能的组合框:

string SelectedItemName = (string)comboBox2.SelectedItem.ToString();
Console.WriteLine(SelectedItemName);
if (comboBox2.SelectedIndex > -1)
{
    testvariabel2.GetSessionName();
}
因此,我检查用户是否从ComboBox中选择了某些内容,然后在另一个类CTestRack.cs中调用函数GetSessionName

Dictionary<string, Dictionary<string, string>> newDictionary = new Dictionary<string,Dictionary<string, string>>();

foreach (SectionData section  in data.Sections)
{
    var keyDictionary = new Dictionary<string, string>();
    foreach (KeyData key in section.Keys)
        keyDictionary.Add(key.KeyName.ToString(), key.Value.ToString());

    newDictionary.Add(section.SectionName.ToString(), keyDictionary);

    if (newDictionary.ContainsKey(testvariabel.SelectedItemName))
    {
        Console.WriteLine("Key: {0}, Value: {1}", keyDictionary[testvariabel.SelectedItemName]);
    }
    else Console.WriteLine("Couldn't check Selected Name");
}
Dictionary newDictionary=newDictionary();
foreach(数据节中的节数据节)
{
var keydedictionary=newdictionary();
foreach(第.Keys节中的KeyData键)
添加(key.KeyName.ToString(),key.Value.ToString());
添加(section.SectionName.ToString(),keyDictionary);
if(newDictionary.ContainsKey(testvariabel.SelectedItemName))
{
WriteLine(“键:{0},值:{1}”,键字典[testvariabel.SelectedItemName]);
}
else Console.WriteLine(“无法检查所选名称”);
}
在这里,我想检查我的字典中是否存在SelectedItemName字符串,但我总是得到Systen.ArgumentNullException,即字符串SelectedItemName在我的CTestRackClass中为NULL

现在我的问题是,如何在CTestRack中搜索另一个类中设置的字符串
Form1?

我发现您的代码有两个明显的问题

  • 您正在检查一个字典(newDictionary)中是否存在密钥,但正在尝试从另一个字典(keyDictionary)中检索该密钥
  • 即使在字典还没有完全建立起来之前,你也要尝试在字典中查找关键字。将if检查移到foreach循环之外

  • 嗯。。。事实上你查字典是对的!要确定字典中是否存在键,请使用
    ContainsKey

    if(myDictionary.ContainsKey(myKey))
    {
        //do something
    }
    
    然而,您的问题来自这样一个事实:null从来都不是字典中的有效键(主要是因为null没有正确的哈希代码)。因此,您需要确保要查找的密钥不为null。从您的代码中,我猜
    testvariabel.SelectedItemName
    没有按应有的方式设置

    此外,还有一种更有效的方法,可以在使用某个值之前查看该值是否存在。使用
    TryGetValue

    TValue val;
    if(myDictionary.TryGetValue(myKey, out val))
    {
        //do something with val
    }
    
    这样您就不需要访问myDictionary[myKey]。如果使用
    ContainsKey
    ,实际上是在两次访问同一个值。这在大多数情况下成本很小,但很容易避免,所以你应该尝试一下


    请注意,我只回答了有关查字典的特定问题。我不能就代码的整体正确性发表任何意见。

    仔细研究一下,我知道null不是有效的键,我还写下了设置变量SelectedItemName的代码:“string SelectedItemName=(string)comboBox2.SelectedItem.ToString();”你认为这是错误的吗?因为通常我想使用字符串SelectedItemName
    String SelectedItemName=(String)comboBox2.SelectedItem.ToString()生成TryGetValue
    不设置属性
    testvariabel。SelectedItemName
    ,它会创建一个新变量,之后您似乎不会使用它。。。