patterncsharpModerate
Managing fault types
Viewed 0 times
faultmanagingtypes
Problem
I have a
And I have a
Now, I want to populate the
Is there a better way to do this?
FaultType enum with more than 100 members:public enum FaultType
{
FaultType1,
FaultType2,
FaultType3,
FaultType4,
FaultType5,
}And I have a
FaultTypeConstants class corresponding to FaultType:public class FaultTypeConstants
{
public const int FaultType1 = 600;
public const int FaultType2 = 100;
public const int FaultType3 = 453;
public const int FaultType4 = 200;
public const int FaultType5 = 300;
}Now, I want to populate the
List based on FaultTypeConstants values:public static List GetFaults(List FaultConstants)
{
var faults = new List();
FaultConstants.ForEach(fc => {
switch (fc)
{
case FaultTypeConstants.FaultType1:
faults.Add(FaultType.FaultType1);
break;
case FaultTypeConstants.FaultType2:
faults.Add(FaultType.FaultType2);
break;
case FaultTypeConstants.FaultType3:
faults.Add(FaultType.FaultType3);
break;
case FaultTypeConstants.FaultType4:
faults.Add(FaultType.FaultType4);
break;
case FaultTypeConstants.FaultType5:
faults.Add(FaultType.FaultType5);
break;
default:
break;
}
});
return faults;
}Is there a better way to do this?
Solution
Get rid of the constants entirely and assign those values to the enum members themselves.
You can then use the enum everywhere.
You can cast an enum member to
You can then use the enum everywhere.
You can cast an enum member to
int to get its numeric value.Context
StackExchange Code Review Q#32309, answer score: 10
Revisions (0)
No revisions yet.