I’m fairly new to C# and I come from a Ruby background. I still have a lot to learn and that’s why I’m asking the following question:
Goal:
1) I would like to create a Dictionary with string as keys and whatever object type I want as values. Something like this:
Dictionary<string, T>
2) But That’s not all. I also want a “Master” Dictionary with string as keys and the Dictionary described above as values.
Something like this:
Dictionary<string, Dictionary<string T>>
I want it to be that way so I can work like the example below:
MasterDictionary[Dict1].Add( Thing1 )
MasterDictionary[Dict2].Add( Thing2 )
Current Code:
I’m trying to achieve this using the following code
public List<string> DataTypes;
public Dictionary<string, Dictionary<string, object>> TempData;
public Dictionary<string, Dictionary<string, object>> GameData;
public Session()
{
// Create a list of all Data Types.
DataTypes = new List<string>();
DataTypes.Add("DataInfo");
DataTypes.Add("Maps");
DataTypes.Add("Tilesets");
// Create and populate TempData dictionary.
TempData = new Dictionary<string, Dictionary<string, object>>();
TempData.Add("DataInfo", new Dictionary<string, DataInfo>());
TempData.Add("Maps", new Dictionary<string, Map>());
TempData.Add("Tilesets", new Dictionary<string, Tileset>());
// Create GameData dictionary and copy TempData into it.
GameData = new Dictionary<string, object>(TempData);
}
Problem:
I get the following errors
1)The best overloaded method match for ‘System.Collections.Generic.Dictionary>.Add(string, System.Collections.Generic.Dictionary)’ has some invalid arguments
2)Error 10 Argument 1: cannot convert from ‘System.Collections.Generic.Dictionary>’ to ‘System.Collections.Generic.IDictionary’
The following lines are underlined in red
TempData.Add("DataInfo", new Dictionary<string, DataInfo>());
TempData.Add("Maps", new Dictionary<string, Map>());
TempData.Add("Tilesets", new Dictionary<string, Tileset>());
// Create GameData dictionary and copy TempData into it.
GameData = new Dictionary<string, object>(TempData);
I clearly did something wrong or even illegal here, I just need to know what it is and how I could fix it!
I’ve been doing a lot of research on my own but found nothing that could help me on this.
I’ve seen how to make a dictionary of dictionaries but I’m not quite sure how to tell a dictionary not to care about the object type in Value.
I know “T” might be of some use here but I don’t really know how to use it since it always tells me “The type or namespace name ‘T’ could not be found”
So what should I do?
Thanks in advance
– Sasha
The problem is that you aren’t matching your typedefs with the objects you create. Change your code to this: