Reputation: 37
I'd like to find a dictionary by using a string in order to copy it into a new dictionary -- seems like it should be simple but I'm not sure of syntax.
Basically it's just:
// the object's myName corresponds to an existing dictionary
string stringdic = myName;
// here's where I try to create a new dictionary containing the values of the existing dictionary
Dictionary<string, int> mySkills = new Dictionary<string, int>(myName);
The goal here is just to create an object with a given string as its "name", and from the name know which out of a set of dictionaries it should use -- for example if myName = Bob, I want to access the dictionary Bob in this script.
Thus some other way of referencing the pre-existing dictionary to get its keys / values would also work. Speed is not a big issue, but I'd like to keep the code simple. Thanks!
Upvotes: 2
Views: 99
Reputation: 65682
Here's how you do a Dictionary of Dictionaries:
var persons = new Dictionary<string, Dictionary<string, int>>
{
{ "Jeremy", new Dictionary<string, int> { { "age", 20 }, { "height_cm", 180 } } },
{ "Evan", new Dictionary<string, int> { { "age", 18 }, { "height_cm", 167 } } },
};
Dictionary<string, int> x = persons["Jeremy"];
x["age"] = 34;
Upvotes: 5
Reputation: 30092
If you need to be able to lookup a dictionary using a string, you can have a dictionary of dictionaries, something like:
using X = Dictionary<string, int>;
var all = new Dictionary<string, X>();
X mySkills = all[myName];
Upvotes: 2