Aroueterra
Aroueterra

Reputation: 377

C# How do you loop through multiple dictionaries?

A standard dictionary would look like this:

        public Dictionary<int, DictionarySetup> H = new Dictionary<int, DictionarySetup>()
        {
            {18000, new DictionarySetup { Some values }},
        };

Ranging from A-T, all of these are in a class called DictionaryInit, right now I check the value if there's a match with this boolean:

public Boolean Dictionary_Test(Dictionary<int, DictionarySetup> AccountLexicon)
    {
        DictionarySetup ClassValues;
        if (AccountLexicon.TryGetValue(MapKey, out ClassValues))
        {
            return true;
        }
        return false;
    }

Now, I'm looking for a more efficient method to loop through each Dictionary and, if there's a match, get that particular dictionary for use in a subsequent method, this is what it looks like now in an if/else:

            if(Dictionary_Test(theDictionary.C) == true)
            {
              Dictionary_Find(Account_String, rowindex, theBSDictionary.C, Cash_Value, txtCurrency.Text);
            }
            else if (Dictionary_Test(theDictionary.D) == true)
            {
                Dictionary_Find(Account_String, rowindex, theDictionary.D, Cash_Value, txtCurrency.Text); //Method that makes use of the dictionary values, above dictionary checks only if it exists
            }

With dictionaries from A-T, that would be alot of if/else's! Is there a better way to do this? I've found one thread mentioning this same topic, by adding the dictionaries to a dictionary array[] then looping over it, but how do I get the name of the matching dictionary if a match is found or make my second method, Dictionary_Find, use the matching dictionary?

Upvotes: 0

Views: 816

Answers (2)

garethb
garethb

Reputation: 4051

Another possible solution, you could use reflection to get each dictionary from A-T from the DictionaryInit class. Create an array that contains values A-T, loop through the array and use reflection to get the dictionary, and test that dictionary, if you find a match, return that dictionary and exit the loop. Something like:

var dicts = new[]{"A", "B", ......., "T"}

foreach (var dict in dicts)
{
    var found = CheckDictionary(theDictionary, dict);
    if (found != null)
    {
        Dictionary_Find(Account_String, rowindex, (Dictionary<int, DictionarySetup>)found, Cash_Value, txtCurrency.Text);
        break;
    }
}

public static object CheckDictionary(object dictClass, string dictName)
{
   var theDictionary = dictClass.GetType().GetProperty(dictName).GetValue(dictClass, null);
    return Dictionary_Test(theDictionary) ? theDictionary : null;
}

I've just quickly grabbed some code from a project I've done and modified it to suit but haven't tested it. Might need a few tweaks but hopefully gets you close!

Upvotes: 1

kurakura88
kurakura88

Reputation: 2305

// put dictionary A ~ T to a list of dictionary
List<Dictionary<int, DictionarySetup>> dictionaries = new List<Dictionary<int, DictionarySetup>>{A,B,C, ... , T}; // Replace ... with D,E,F, etc. until T
// iterate each dictionary and if found, exit the loop
foreach(var dict in dictionaries)
{
   if(Dictionary_Test(dict))
   {
      Dictionary_Find(Account_String, rowindex, dict, Cash_Value, txtCurrency.Text);
      break;
   }
}

Upvotes: 0

Related Questions