Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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#_Regex_Wpf_Combobox - Fatal编程技术网

C# 当以某种方式将两个以上的项存储到字典中时,程序崩溃

C# 当以某种方式将两个以上的项存储到字典中时,程序崩溃,c#,regex,wpf,combobox,C#,Regex,Wpf,Combobox,很难很好地描述标题中的情况,但这里是要点 我有一个组合框,里面充满了来自第一本字典的codebehind的复选框然后我从组合框中提取文本,然后根据键(组合框文本块中的名称)查找值它们是逗号分隔的,因此我使用正则表达式分隔值 第一个可以正常工作,然后添加第二个。但当我试图添加更多时,它就爆炸了 Dictionary<string, string> selectedCharacters = new Dictionary<string, string>(); priv

很难很好地描述标题中的情况,但这里是要点

我有一个组合框,里面充满了来自第一本字典的codebehind的复选框
然后我从组合框中提取文本,然后根据键(组合框文本块中的名称)查找值
它们是逗号分隔的,因此我使用正则表达式分隔值

第一个可以正常工作,然后添加第二个。但当我试图添加更多时,它就爆炸了

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

    private void button1_Click(object sender, RoutedEventArgs e)
    {   
        string[] w = SplitWords(MC.Text);
        selectedCharacters.Clear();

        foreach (string s in w)
        {
            string fileName = "";
            Items.TryGetValue(s, out fileName);
            selectedCharacters.Add(s, fileName);
        }

        foreach (var item in selectedCharacters)
        {
            testBlock.Text += string.Format(item.Key + "   " +
            item.Value + "\n");
        }
    }

    static string[] SplitWords(string s)
    {
        return Regex.Split(s, @"(.*?),");
    }
Dictionary selectedCharacters=new Dictionary();
私有无效按钮1\u单击(对象发送者,路由目标)
{   
字符串[]w=SplitWords(MC.Text);
selectedCharacters.Clear();
foreach(w中的字符串s)
{
字符串fileName=“”;
Items.TryGetValue(s,out文件名);
所选字符。添加(s,文件名);
}
foreach(选定字符中的变量项)
{
testBlock.Text+=string.Format(item.Key+“”+
项目值+“\n”);
}
}
静态字符串[]拆分字(字符串s)
{
返回正则表达式拆分,@“(.*)”;
}
testBlock是我用来在屏幕上显示它的文本块。我在名称/键中还有其他字符,如a'和一些空格等等,所以我骗了一个正则表达式,它会寻找逗号分隔


我觉得这很奇怪,2是好的,但3是正确的

在浏览了注释部分之后,我注意到问题在于重复键。这个问题是由于
regex
故障造成的,如回答的后半部分所述

ArgumentException:字典中已存在具有相同键的元素

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

private void button1_Click(object sender, RoutedEventArgs e)
{   
    string[] w = SplitWords(MC.Text);
    selectedCharacters.Clear();

    foreach (string s in w)
    {
        // Check whether KEY exists  
        if(!selectedCharacters.ContainsKey(s)){
            string fileName = "";
            Items.TryGetValue(s, out fileName);
            selectedCharacters.Add(s, fileName);
        }
    }

    foreach (var item in selectedCharacters)
    {
        testBlock.Text += string.Format(item.Key + "   " +
        item.Value + "\n");
     }
 }

static string[] SplitWords(string s)
{
    return s.Split(','); //It would do the same as regex
}
解决方案是在将密钥添加到字典之前检查密钥是否存在

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

private void button1_Click(object sender, RoutedEventArgs e)
{   
    string[] w = SplitWords(MC.Text);
    selectedCharacters.Clear();

    foreach (string s in w)
    {
        // Check whether KEY exists  
        if(!selectedCharacters.ContainsKey(s)){
            string fileName = "";
            Items.TryGetValue(s, out fileName);
            selectedCharacters.Add(s, fileName);
        }
    }

    foreach (var item in selectedCharacters)
    {
        testBlock.Text += string.Format(item.Key + "   " +
        item.Value + "\n");
     }
 }

static string[] SplitWords(string s)
{
    return s.Split(','); //It would do the same as regex
}
输出:输出错误,空字符串如下

word : 
word : me
word :
word : you
(...)
解决方案:使用
String.Split()


附加:检查关于使用正则表达式进行拆分的答案

w中的每个字符都是唯一的吗?否则,您可能会得到一个重复的密钥异常。@失败编程它们都是唯一的。它们基本上是从现有词典中使用的,没有一个名称是相同的。@hatchet我认为如果返回true,它不会抱怨,因为检查是在不同的词典(项)上进行的,但这一逻辑似乎相当合理weird@failedprogramming我已经通过调试器运行了它,它告诉我的最好消息是ArguementException,我正在输入一个重复的密钥…尽管它不应该这样做。当我只在文本框中显示s而不向所选字符添加任何内容时,没有重复项。伙计们,正则表达式是错误的。。测试后查看答案:)@Brian Crawford查看给出的答案完美!为什么正则表达式这么难?我不知道string.split()方法,所以我会记住它sure@Brian克劳福德:你需要一点时间来掌握正则表达式。[即使我对正则表达式也不是十全十美;)]当你有正则表达式时,最好使用简单的方法:)。。很高兴能帮助你。!
String[] arr=  test.Split(',');