I was wondering if there was a more efficient (efficient, as in simpler / cleaner code) way to make a case statement like below ...
I have a dictionary. Its key type is Enum, and its value type is bool. If the boolean value is true, I want to change the color of the label on the form.
Variable names have been changed as an example.
Dictionary<String, CustomType> testDict = new Dictionary<String, CustomType>(); //populate testDict here... Dictionary<MyEnum, bool> enumInfo = testDict[someString].GetEnumInfo(); //GetEnumInfo is a function that iterates through a Dictionary<String, CustomType> //and returns a Dictionary<MyEnum, bool> foreach (KeyValuePair<MyEnum, bool> kvp in enumInfo) { switch (kvp.Key) { case MyEnum.Enum1: if (someDictionary[kvp.Key] == true) { Label1.ForeColor = Color.LimeGreen; } else { Label1.ForeColor = Color.Red; } break; case MyEnum.Enum2: if (someDictionary[kvp.Key] == true) { Label2.ForeColor = Color.LimeGreen; } else { Label2.ForeColor = Color.Red; } break; } }
So far, MyEnum has 8 different meanings. This means that I have 8 different cases. I know that there should be an easier way to do this, I just cannot conceptualize it in my head.
If anyone could help, I would really appreciate it. I love C #, and every day I learn new things. I swallow it like a sponge :)
-CP
source share