0

I'm really stuck here now with my Dictionary Output:

I have this Code to "fill" the dictionary (basically there are 2 of them):

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

Form1 _form1Object = new Form1();

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

This works pretty well, but now I would like to search through it. That means I have a String which stores a "selection" from the "combobox" in Form1.class I have.

Now I want to lookup that String in my Dictionary, for that I have the following:

while (_form1Object.comboBox2.SelectedIndex > -1)
{
     _form1Object.SelectedItemName = _form1Object.comboBox2.SelectedItem.ToString();

     if (newDictionary.ContainsKey(_form1Object.SelectedItemName))
     {
         Console.WriteLine("Key: {0}, Value: {1}", newDictionary[_form1Object.SelectedItemName]);
         Console.WriteLine("Dictionary includes 'SelectedItem' but there is no output");
     }
     else Console.WriteLine("Couldn't check Selected Name");
}

But, yes you're right, it doesn't work, the output in the console is:

System.Collections.Generic.Dictionary`2[System.String,System.String]

And I even don't get any of my Console.WriteLine("Couln't check selected Name") So that mean it will run through the IF statement but the Console.WriteLine function is not working.

Now my question, how do I lookup my String SelectedItemName in the Dictionary<string, Dictionary<string,string>>?

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Luca
  • 1,766
  • 3
  • 27
  • 38
  • Please format your code properly. – Yuval Itzchakov Sep 11 '14 at 13:59
  • 2
    It is working; the value of a key in the outer dictionary is another dictionary object, and `ToString()` on a dictionary uses the default behavior from `System.Object`: print out the name of the object's type. – cdhowie Sep 11 '14 at 13:59
  • `while (_form1Object.comboBox2.SelectedIndex > -1)` ???? – James Sep 11 '14 at 14:02
  • This while checks if the user has selected something in the Combobox, if yes, it will go through the content of the while loop, or am I wrong? – Luca Sep 11 '14 at 14:03
  • I suppose newDictionary[_form1Object.SelectedItemName] returns your keyDictionary you filled before. You have to add the second index of your actual key newDictionary[_form1Object.SelectedItemName][index] – MakePeaceGreatAgain Sep 11 '14 at 14:03

3 Answers3

2

You need something similar to:

foreach (var keyValue in newDictionary[_form1Object.SelectedItemName])
{
   Console.WriteLine("Key: {0}, Value: {1}", keyValue.Key, keyValue.Value);
}
decPL
  • 5,384
  • 1
  • 26
  • 36
2

You'll have to implement your own output logic. Dictionary<TKey, TValue> does not override Object.ToString so the output is just the class name. Something like:

public static class DictionaryExtensions
{ 
  public static string WriteContent(this Dictionary<string, string> source)
  {
    var sb = new StringBuilder();
    foreach (var kvp in source) {
      sb.AddLine("Key: {0} Value: {1}", kvp.Key, kvp.Value);
    }
    return sb.ToString();
  }
}

Will allow you to just call .WriteContent() on newDictionary[_form1object.SelectedItemName] when that namespace is referenced.

tom.dietrich
  • 8,219
  • 2
  • 39
  • 56
1

It is working as it should work. Look you are fetching this expression

newDictionary[_form1Object.SelectedItemName]

where newDictionary has a string key and another dictionary as the value. So this expression will return you the value field of the parent dictionary which is actually a dictionary.

That means you also have to iterate over your child dictionary like this

 if (newDictionary.ContainsKey(_form1Object.SelectedItemName))
 {
     Console.WriteLine("Parent Key : {0}",_form1Object.SelectedItemName)
     foreach(var childDict in newDictionary[_form1Object.SelectedItemName])
     {
        Console.WriteLine("Key: {0}, Value: {1}", childDict.Key,childDict.Value);
     }
 }
Sachin
  • 40,216
  • 7
  • 90
  • 102