0

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. 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

3
  • 2
    You only have one element within your Dictionary because your code only ever runs once. Commented Jul 19, 2011 at 17:01
  • From what I can see, you're only adding 1 pair to a new dictionary, I would only expect it to have 1 item in it. Commented Jul 19, 2011 at 17:03
  • Perhaps it might be better to explain what you're trying to achieve, there are some issues in using a dictionary that you may not realise. Commented Jul 19, 2011 at 17:05

4 Answers 4

1
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you it works perfectly. And that must be record time for answering a question. I can't believe I didn't see that.
@oakley: Glad it worked for you! If this solved your issue, please don't forget to mark it as the accepted answer so that others with similar issues will be able to find the solution easily.
1

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)."); 
        }
    }

}

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.