0

I have a static class to hold a dictionary and 2 get methods to access it

Here is my class:


public static class ConfiguraCuadros
    {    
    public static Dictionary<string,Dictionary<string,Dictionary<string,Dictionary<string,string>>>> GetDictionary()
       {
           // Try to get the result in the static Dictionary
           return _configcuadros;
       }

       public static Dictionary<string, Dictionary<string, Dictionary<string, string>>> GetHoja(string key)
       {
           // Try to get the result in the static Dictionary
           Dictionary<string, Dictionary<string, Dictionary<string, string>>> result = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
           if (_configcuadros.TryGetValue(key, out result))
           {
               return result;
           }
           else
           {
               return null;
           }
       }

    public static readonly Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> _configcuadros = new Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> 
    {

     {   "Formato01",   //this is just a hint, the dictionary is much more extensive
                new Dictionary<string, Dictionary<string, Dictionary<string, string>>> 
                {
                    {
                        "F01C01A",      
                        new Dictionary<string, Dictionary<string, string>> 
                        {
                            {
                                "X",
                                new Dictionary<string, string>
                                {
                                    { "key1" , "value1" },
                                    { "key2" , "value2" },
                                    { "key3" , "value3" },
                                }
                            },
                        }
                    },

                }

            },
        }
    }`

When I use the getter method,

ConfiguraCuadros.GetDictionary();

It throws an exception:

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
'ConfiguraCuadros.GetDictionary()' threw an exception of type 'System.TypeInitializationException'
    base: {"The type initializer for 'beDGRAIC.ConfiguraCuadros' threw an exception."}
    TypeName: "beDGRAIC.ConfiguraCuadros"

or

'ConfiguraCuadros.GetHoja("Formato01")' threw an exception of type 'System.TypeInitializationException'
    base: {"The type initializer for 'beDGRAIC.ConfiguraCuadros' threw an exception."}
    TypeName: "beDGRAIC.ConfiguraCuadros"

As you can see, my intention is to have a static dictionary. I think the problem is in the dictionary declaration ... but I can't see where...

Just in case, "beDGRAIC" is my namespace.

Thanks for your help!

culebrin
  • 125
  • 1
  • 15

1 Answers1

1

Your code works (almost) as is for me.

I just added a missing semi-colon to get it to compile but can then call ConfiguraCuadros.GetDictionary(); without any exception.

Here is the code back with that missing semicolon:

public static class ConfiguraCuadros
{
    public static Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> GetDictionary()
    {
        // Try to get the result in the static Dictionary
        return _configcuadros;
    }

    public static Dictionary<string, Dictionary<string, Dictionary<string, string>>> GetHoja(string key)
    {
        // Try to get the result in the static Dictionary
        Dictionary<string, Dictionary<string, Dictionary<string, string>>> result = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
        if (_configcuadros.TryGetValue(key, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }

    public static readonly Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> _configcuadros = new Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, string>>>> 
        {
            {   "Formato01",   //this is just a hint, the dictionary is much more extensive
                new Dictionary<string, Dictionary<string, Dictionary<string, string>>> 
                    {
                        {
                            "F01C01A",      
                            new Dictionary<string, Dictionary<string, string>> 
                            {
                                {
                                    "X",
                                    new Dictionary<string, string>
                                    {
                                        { "key1" , "value1" },
                                        { "key2" , "value2" },
                                        { "key3" , "value3" },
                                    }
                                },
                            }
                        }
                     }
              }
        };
}

[UPDATE]

I do agree with the comments above about checking out the InnerException as a general rule for a type initialisation exception and, particularly, about the unfriendly nature of the datatype!

Stewart_R
  • 13,764
  • 11
  • 60
  • 106
  • I was having problem reading the exact innerexception, because that call is in a method, and only that method called was in a try-catch bracket, and yes, the answer was in the innerexception. – culebrin Oct 29 '14 at 21:42
  • Now i have a strange problem with double quoted string in one dictionary `{ "TXT", "(3/) ""A""." },`, the compiler underlines `"A""."` and I don't know why... (it says '} expected') – culebrin Oct 29 '14 at 23:20
  • It's not clear to the compiler(or me!) what your string is. You need to escape the " characters if including them in a string literal. – Stewart_R Oct 30 '14 at 00:38
  • I am trying to escape them, the original string is: `(3/) "A".` but it seems my approach has some mistake that I cannot see – culebrin Oct 30 '14 at 14:00