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.