开发者

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];
            str开发者_开发技巧ing 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. 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. 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 (e.g. from the command line, when they click a button, etc.). If this is the case, you need to have your keylist dictionary at a higher level (as an instance variable, for example). The way the code is now, every time the function is called a new dictionary is created and the key is added to it; 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.


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.

Instead try moving that line outside of the function. Since you are using winforms, you can create a class level variable, something like this:

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:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜