Is there a more efficient way to run enum values ​​through a switch-case statement in C # than this?

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

+4
source share
5 answers

You can simply change the label and then set the colors after the switch statement:

 Label label = null; switch (kvp.Key) { case MyEnum.Enum1: label = Label1; break; case MyEnum.Enum2: label = Label2; break; } label.ForeColor = kvp.Value ? Color.LimeGreen : Color.Red; 

Alternatively, you can have a Dictionary<MyEnum, Label> and just find the label accordingly:

 labelDictionary[kvp.Key].ForeColor = kvp.Value ? Color.LimeGreen : Color.Red; 
+7
source

Create a hash table that maps the values ​​of your enumeration to a label. Then you can just look at the shortcut to change it, rather than switch it.

+2
source

Create a map between the enumeration and the label, which should be changed based on this enumeration value. Then use the current enumeration value to update the corresponding label.

 Dictionary<MyEnum,Label> labelMap = new Dictionary<MyEnum,Label>(); labelMap.Add( MyEnum.Enum1, Label1 ); ... foreach (KeyValuePair<MyEnum, bool> kvp in enumInfo) { var label = labelMap[kvp.Key]; label.ForeColor = kvp.Value ? Color.LimeGreen : Color.Red; } 
+2
source

First, you can combine the body of case statements into a function that takes one or more parameters. This may be easier than duplicating content. @ The idea of ​​choosing the right instance of the shortcut in the switch statement is also a good idea, and if the logic changes only on that basis, it is better to choose.

You can also use the ?: operator to simplify the logic of the solution.

For instance:

 switch (kvp.Key) { case MyEnum.Enum1: UpdateUI( Label1, someDictionary[kvp.Key] ); break; case MyEnum.Enum2: UpdateUI( Label2, someDictionary[kvp.Key] ); break; case MyEnum.Enum3: UpdateUI( Label3, someDictionary[kvp.Key] ); break; } public void UpdateUI( Label theLabel, bool whichOne ) { theLabel.ForeColor = whichOne ? Color.LimeGreen : Color.Red; } 
+1
source

You can simply put your words in a dictionary:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace ConsoleApplication3 { class Program { private enum MyEnum { shwan, dooo, sieven, sieven_haif, shwenty, shwenty_doo_haif, schfifty_faive } class EnumInfo { public MyEnum Enum; public bool State; public override bool Equals(object obj) { EnumInfo ei = obj as EnumInfo; return this.Enum == ei.Enum && this.State == ei.State; } public override int GetHashCode() { return this.Enum.GetHashCode() ^ this.State.GetHashCode(); } } private static IDictionary<EnumInfo, Color> EnumColorDict = new Dictionary<EnumInfo, Color>() { {new EnumInfo(){Enum=MyEnum.shwan, State=true},Color.LimeGreen}, {new EnumInfo(){Enum=MyEnum.shwan, State=false},Color.Red}, {new EnumInfo(){Enum=MyEnum.dooo, State=true},Color.LimeGreen}, {new EnumInfo(){Enum=MyEnum.dooo, State=false},Color.Red} }; static void Main(string[] args) { EnumInfo ei = new EnumInfo() { Enum = MyEnum.shwan, State = true }; Color c = EnumColorDict[ei]; } } } 
0
source

Source: https://habr.com/ru/post/1305571/


All Articles