Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dictionary using TValue within another Dictionary

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

like image 825
Sasha Avatar asked Oct 11 '22 17:10

Sasha


2 Answers

you could change all the dictonaries to string,object ....

but maybe, since you are from Ruby, there's another problem going on.....

putting everything into dictionaries then having to work out what types they are again when extracting is perhaps not the best approach. In typed languages, its often best to make types for everything

perhaps it would be better to have a typed object with the dictionaries of the type they are...

class GameResources
{
    public Dictionary<string, Map> Maps { get; set;}
    public Dictoinary<string, Tileset> Tileset { get; set; }
}

etc... or quite possibly with a bit of thought, another class structure might be more appropriate in your situation

like image 119
Keith Nicholas Avatar answered Oct 13 '22 08:10

Keith Nicholas


The problem is that you aren't matching your typedefs with the objects you create. Change your code to this:

public void 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, object>());
    TempData.Add("Maps", new Dictionary<string, object>());
    TempData.Add("Tilesets", new Dictionary<string, object>());

    // Create GameData dictionary and copy TempData into it.
    GameData = new Dictionary<string, Dictionary<string, object>>(TempData);
}
like image 23
Ed Bayiates Avatar answered Oct 13 '22 08:10

Ed Bayiates