I've been exploring dynamic dictionaries as a potential solution to associating multiple strings with a single 'ID' string. What I need to do is assign a series of 4-letter text strings to a key that is also a 4-letter text string (each string is unique with a range of AAAA-ZZZZ).
The keys are 4-letter text strings like: "AAAA", "AAGB", etc. Each key can hold multiple 4-letter text strings: "ACFE", "AAFE", etc.
The goal is to select a key and then randomly select one of the dynamic values inside it. My current setup for testing is as follows:
dynamic d1 = new System.Dynamic.ExpandoObject(); // Dynamic objects
dynamic d2 = new System.Dynamic.ExpandoObject();
dynamic d3 = new System.Dynamic.ExpandoObject();
var dict = new Dictionary<string, dynamic>();
// Assign dynamic objects to the Dictionary Key
dict["AAAA"] = d1;
dict["AAGB"] = d2;
dict["ABDA"] = d3;
// Populate the dynamic object with the value(s)
dict["AAAA"].FooBar = new { i1 = "ACFE", i2 = "AAFE", i3 = "CAED", i4 = "HSGB" };
dict["AAGB"].FooBar = new { i1 = "ZZZZ", i2 = "XXXX", i3 = "CAED", i4 = "HSGB" };
dict["ABDA"].FooBar = new { i1 = "YYYY", i2 = "WWWW", i3 = "CAED", i4 = "HSGB" };
string newName = dict["ABDA"].FooBar.i1;
return newName;
That pulls through the correct string. But is there a way of setting this up so I can pull one of the values out at random instead of having to explicitly specify which value I'm after? Or should I change approach altogether (I've done something similar to this using jagged arrays and will use that as a fallback but was curious about this dynamic dictionary as a possible alternative).
Aucun commentaire:
Enregistrer un commentaire