I'm trying to create a Dictionary with array of integer, string and boolean data types as values. I figured, I should use object[] as value, so declaration would look so:
Dictionary<long, object[]> netObjectArray = new Dictionary<long, object[]>();
Whenever I try setting its element's value to something, VS says there was no such key found in the dictionary.
netObjectArray[key][2] = val; // ex: The given key was not present in the dictionary.
How do I work with this correctly?
UPD1: Somehow, right before throwing this exception, the other dictionary is used without problems in a similar way:
Dictionary<long, Vector2> netPositions = new Dictionary<long, Vector2>();
netPositions[key] = new Vector2(x, y); // works ok
After this locals show the value was assigned and dictionary now contains that entry. Why is this not the case with my other dictionary?
Solution: Before writing a value to an array of values, we must first initialize that array. This code works for me:
try { netObjectArray[key] = netObjectArray[key]; } // if the object is undefined,
catch { netObjectArray[key] = new object[123]; } // this part will create an object
netObjectArray[key][0] = new Vector2(x, y) as object; // and now we can assign a value to it :)
netObjectArray[key]to at leastnew object[2]?Tuple<int,string,bool>as value instead. msdn.microsoft.com/en-us/library/system.tuple.aspxPlayerclass with all of that info, and have a Dictionary ofPlayers.