简体   繁体   English

在 c# 中添加到字典时出现问题

[英]Problem adding to dictionary in c#

Alright so I've looked hard but I couldn't seem to find answer to my problem.好吧,所以我看起来很努力,但我似乎无法找到我的问题的答案。 There must be a problem in my code and it would be really helpful if someone could look at it for me.我的代码中一定有问题,如果有人可以帮我看一下,那将非常有帮助。

       Dictionary<string, string> keylist = new Dictionary<string, string>();
            if (intext.Contains("addkey") && intext.Contains("def"))
        {

            string[] keywords = intext.Split(' ');
            string key1 = keywords[1];
            string def2 = keywords[3];
            string fkey = key1.Replace("_", " ");
            string fdef = def2.Replace("_", " ");
            keylist.Add(fkey, fdef);
            say("Phrase '" + fkey + "' added with response '" + fdef + "'");
            say("Your Dictionary contains " + keylist.Count.ToString() + " word(s).");
            //////////////////////////////



        }

All I want it to do is take the input in the form of "addkey key_here def definition_here" and add it to the dictionary.我想要它做的就是以“addkey key_here def definition_here”的形式输入并将其添加到字典中。 I added the counting part for debugging purposes and it always says I only have 1 word in the dictionary no matter how many I have added.我添加了计数部分以用于调试目的,它总是说我在字典中只有 1 个单词,无论我添加了多少。 You can probably tell I'm new so please be gentle.你可能会说我是新人,所以请温柔一点。 Thanks谢谢

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

I'm assuming that this function is called whenever the user enters some sort of command (eg from the command line, when they click a button, etc.).我假设只要用户输入某种命令(例如,从命令行,当他们单击按钮等),就会调用这个 function。 If this is the case, you need to have your keylist dictionary at a higher level (as an instance variable, for example).如果是这种情况,您需要将键keylist字典置于更高级别(例如,作为实例变量)。 The way the code is now, every time the function is called a new dictionary is created and the key is added to it;现在的代码方式是,每次调用 function 时都会创建一个新字典并将密钥添加到其中; this is why there's only one.这就是为什么只有一个。

At the risk of misjudging or oversimplifying your problem, just moving the line I quoted above outside of the function body should help.冒着误判或过度简化您的问题的风险,只需将我上面引用的行移到 function 主体之外应该会有所帮助。

In the code as given you are recreating the dictionary every time you run it.在给定的代码中,您每次运行时都在重新创建字典。

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

Reinitializes the variable keylist to an empty dictionary.将变量 keylist 重新初始化为空字典。

Instead try moving that line outside of the function.而是尝试将该行移到 function 之外。 Since you are using winforms, you can create a class level variable, something like this:由于您使用的是 winforms,因此您可以创建一个 class 级别变量,如下所示:

public partial class Form1 : Form
{
    Dictionary<string, string> keylist = new Dictionary<string, string>();       

    public Form1()
    {
        InitializeComponent();
    }

    public void YourFunction(string intext)
    {
        if (intext.Contains("addkey") && intext.Contains("def"))        
        {            
            string[] keywords = intext.Split(' ');            
            string key1 = keywords[1];            
            string def2 = keywords[3];            
            string fkey = key1.Replace("_", " ");            
            string fdef = def2.Replace("_", " ");            
            keylist.Add(fkey, fdef);            
            say("Phrase '" + fkey + "' added with response '" + fdef + "'");            
            say("Your Dictionary contains " + keylist.Count.ToString() + " word(s)."); 
        }
    }

}

You need to loop through the if (intext.Contains...{} block or it only runs once. If you are looping through it and only getting one entry in the Dictionary, it's because you keep reassigning it within the scope in a scenario such as:您需要遍历if (intext.Contains...{}块,否则它只运行一次。如果您循环遍历它并且只在 Dictionary 中获得一个条目,那是因为您在某个场景中不断在 scope 中重新分配它如:

while(true) {
    Dictionary<string, string> keylist = new Dictionary<string,string>();
    //...
}

So you'll need to move it outside, if that's your case.所以你需要把它移到外面,如果你是这样的话。

I'm not sure exactly what your input string is or how you need to use it, though I think you're looking to split the input string and loop through along this line...我不确定您的输入字符串是什么或您需要如何使用它,但我认为您正在寻找拆分输入字符串并沿着这条线循环......

private Dictionary<string, string> GetData(string intext)
{
    Dictionary<string, string> keylist = new Dictionary<string, string>();

    string[] keywords = intext.Split(' ');
    foreach (string s in keywords)
    {
        if (s.Contains("addkey") && s.Contains("def"))
        {
            string fkey = key1.Replace("_", " ");
            string fdef = def2.Replace("_", " ");

            keylist.Add(fkey, fdef);
            say("Phrase '" + fkey + "' added with response '" + fdef + "'");
            say("Your Dictionary contains " + keylist.Count.ToString() + " word(s).");
        }
    }

    return keylist;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM