0

I want to create a nested ConcurrentDictionary after parsing a large XML file. What I get when running the code below is that the innermost(tmpDictionaryDid) dictionary is null.

I guess I don't save the dictionaries properly or load them incorrectly, but I can't figure out what is incorrect.

Is it even possible to solve it with ConcurrentDictionary or should I try with another solution?

public void temp22db()
    {
        ConcurrentDictionary<string, ConcurrentDictionary<string, string>> dictionary22 = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();
        try
        {
            ConcurrentDictionary<string, string> did = new ConcurrentDictionary<string, string>();

            did.TryAdd("F186 1", "test1");
            did.TryAdd("F186 2", "test2");
            dictionary22.TryAdd("F186", did);

            did.TryAdd("EDA0 1", "test3");
            did.TryAdd("EDA0 2", "test4");
            dictionary22.TryAdd("EDA0", did);

            string test;
            did.TryGetValue("EDA0 1", out test);
            Helper.logInWindow("This works " + test);
        }
        catch (Exception e)
        {
            Helper.logInWindow("SDDBTemp: " + e.Message);
        }
        ecuDictionary2.TryAdd("1638", dictionary22);
    }

public string getDIDInterpretation()
    {
        string outStr = "";
        ConcurrentDictionary<string, ConcurrentDictionary<string, string>> tmpDictionary;
        ecuDictionary2.TryGetValue("1638", out tmpDictionary);

        if (tmpDictionary != null)
        {
            ConcurrentDictionary<string, string> tmpDictionaryDid;
            tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid);

            if (tmpDictionaryDid != null)
            {
                tmpDictionaryDid.TryGetValue("EDA0 1", out outStr);
            }
        }

        Helper.logInWindow("Why U no work? " + outStr);
        return outStr;
    }
dcastro
  • 66,540
  • 21
  • 145
  • 155
Fousk
  • 31
  • 4
  • 3
    You should try to avoid nesting the concurrentdictionaries - you'll run into more concurrency issues with that. Try to combine what would be nested keys into only one key, eg instead of dict[key1][subkey1], store it as dict[key1+subkey1] – jaminto Jun 26 '14 at 03:22

1 Answers1

0

You should be using it like this instead: (The parent ConcurrentDictionary will allow concurrency to your internal dictionaries)

    public static void temp22db()
    {
        Dictionary<string, Dictionary<string, string>> dictionary22 = new Dictionary<string, Dictionary<string, string>>();
        try
        {
            Dictionary<string, string> did = new Dictionary<string, string>();

            did.Add("F186 1", "test1");
            did.Add("F186 2", "test2");
            dictionary22.Add("F186", did);

            did.Add("EDA0 1", "test3");
            did.Add("EDA0 2", "test4");
            dictionary22.Add("EDA0", did);

            string test;
            did.TryGetValue("EDA0 1", out test);
            Helper.logInWindow("This works " + test);
        }
        catch (Exception e)
        {
            Helper.logInWindow("SDDBTemp: " + e.Message);
        }
        ecuDictionary2.TryAdd("1638", dictionary22);
    }

    public static string getDIDInterpretation()
    {
        string outStr = "";
        Dictionary<string, Dictionary<string, string>> tmpDictionary;
        ecuDictionary2.TryGetValue("1638", out tmpDictionary);

        if (tmpDictionary != null)
        {
            Dictionary<string, string> tmpDictionaryDid;
            tmpDictionary.TryGetValue("EDA0", out tmpDictionaryDid);

            if (tmpDictionaryDid != null)
            {
                tmpDictionaryDid.TryGetValue("EDA0 1", out outStr);
            }
        }

        Helper.logInWindow("Why U no work? " + outStr);
        return outStr;
    }
Aldracor
  • 2,133
  • 1
  • 19
  • 27