-1

While working on a nested dictionary, I was warned that the code won't compile since the "Dictionary does not contain a definition for 'CONST_NAME'". The dictionary is declared on a Static constructor in the same class, so that isn't the issue.

The code, in short, is the following:

const int OBJECT_ID1 = 0;
const int OBJECT_ID2 = 1;
const int OBJECT_ID3 = 2;

(...)

static MyCLass()
{
   MyDictionary = new Dictionary<int, Dictionary<int, float>>
   {
      OBJECT_ID1 = new Dictionary<int, float>
      {
         OBJECT_ID2 = 5.0f,
         OBJECT_ID3 = 15.0f
      }
   };
}

public static readonly Dictionary<int, Dictionary<int, float>> MyDictionary;

All three constants have this error, so it isn't because of the nested dictionary (I think).

I am well aware that I can use the piece of code below instead (and it works), however, I want to avoid declaring a Dictionary variable since it won't be used for anything else later on (and I believe the original code is cleaner):

MyDictionary = new Dictionary<int, Dictionary<int, float>>();
Dictionary<int, float> dict = new Dictionary<int, float>();
dict.Add(OBJECT_ID2, 5.0f);
dict.Add(OBJECT_ID3, 15.0f);
MyDictionary.Add(OBJECT_ID1, dict);

So, my questions are: why does the "Dictionary does not contain a definition for 'CONST_NAME'" error keeps occurring, while it works just fine in the second piece of code? Is there a way to make them work as I intended?

I'd also like to point out that straightforwardly replacing these constants with their values is something I want to refrain from doing, since they are prone for modification in the future.

Of course, if that is an impassable limitation, I will have to resort to the second piece of code, although I find it that much dirtier and somewhat unintelligible.

Thank you.

Cacangale
  • 9
  • 5
  • It´s not because of the `const`, but just because of your syntax. Writing something like `Key = Value` simply is no valid c#-code. `myDict[key] = value` however is. – MakePeaceGreatAgain Oct 17 '19 at 19:10
  • If you want something similar to your syntax, try the C# 6 dictionary initializer syntax: https://visualstudiomagazine.com/Blogs/Tool-Tracker/2015/04/C-Sharp-Dictionary-Initializers.aspx – Daniel Crha Oct 17 '19 at 19:13
  • Currently your code has 3 attemts to set a compile time constant past compilation time. In 2 of those, you try to set the int constant to a float value. In one of those, you try to assign a Dictonary to a int constant. So nothing in there is even close to making sense for me. And I know half a dozen programming languages. – Christopher Oct 17 '19 at 19:24
  • For some reason I thought that was how assigning values to a dictionary worked. It's a mere syntax issue, I fixed it following @kristech's answer. I started working with dictionaries recently, so I didn't know how I should properly declare one. – Cacangale Oct 17 '19 at 19:47

1 Answers1

0

Here is proper syntax to create a dictionary like the one you are trying:

            const int OBJECT_ID1 = 0;
            const int OBJECT_ID2 = 1;
            const int OBJECT_ID3 = 2;

            Dictionary<int, Dictionary<int, float>> MyDict = new Dictionary<int, Dictionary<int, float>>()
            {
                {
                    OBJECT_ID1, new Dictionary<int, float>()
                    {
                        { OBJECT_ID2, 5.0f },
                        { OBJECT_ID3, 15.0f }
                    }
                }
            };
Icculus018
  • 1,018
  • 11
  • 19
  • Interesting, I've never seen that syntax before... to be honest, I started working with dictionaries recently, so I didn't know its proper declaration syntax. Thank you for the answer! – Cacangale Oct 17 '19 at 19:43