Reputation: 139
In my program I have an Enum divided in "Group" like (renamed for clarity):
public enum FlowSteps
{
GROUP1_Step1,
GROUP1_Step2,
GROUP2_Step1,
GROUP1_Step2,
...
}
Then I my views I check the current step like:
FlowSteps currentStep = ...;
if (currentStep == GROUP1_Step1) { //Do something }
Now, I wanted to refactor a little this kind of grouping that i don't really like, and I was thiking something like:
public class FlowSteps
{
public enum GROUP1
{
Step1,
Step2
}
public enum GROUP2
{
Step1,
Step2
}
}
Much clearer, but now I cannot compare my currentStep by doing:
FlowSteps currentStep = ...;
if (currentStep == FlowSteps.GROUP1.Step1) { //Do something }
Any suggestion on how to keep this kind of grouping of my Enum and being able to compare the class without adding too much complexity?
How would you proceed?
Upvotes: 2
Views: 110
Reputation: 1571
Patrick Hofman's answer is on target. However, if you needed to keep the enums then you could solve this by using static, readonly classes to wrap them up. This would allow you to retain a common set of well known steps that each group could contain.
enum Steps {
Step1,
Step2,
Step3
};
static class Group1 {
static readonly KeyValuePair<int, string> Step1 =
new KeyValuePair<int, string>(
(int)Types.Group1_Value1,
Types.Group1_Value1.ToString());
static readonly KeyValuePair<int, string> Step2 =
new KeyValuePair<int, string>(
(int)Types.Group1_Value1,
Types.Group1_Value2.ToString());
}
static class Group2 {
public static readonly KeyValuePair<int, string> Step1 =
new KeyValuePair<int, string>(
(int)Steps.Step1,
Steps.Step1.ToString());
public static readonly KeyValuePair<int, string> Step2 =
new KeyValuePair<int, string>(
(int)Steps.Step2,
"Step Two's Other Sneaky Name");
public static readonly KeyValuePair<int, string> Step3 =
new KeyValuePair<int, string>(
(int)Steps.Step3,
Steps.Step3.ToString());
}
Comparisons can then be made against their values, and alternate names for the steps could be substituted - as with Group2.Step2 above.
Group1.Step1.Value == Group2.Step1.Value
Taking this a step further, you could then place each group in a collection of List<NameValuePair<int, string>>
if you need to enumerate or serialize them.
Upvotes: 0
Reputation: 156948
I think a list of enums isn't the right approach here. I would use a set of classes with integers, making them produce unique numbers.
public static class FlowSteps
{
private const int GroupSize = 100;
public static class Group1
{
private const int GroupId = 1;
public const int Step1 = FlowSteps.GroupSize * GroupId + 1;
public const int Step2 = FlowSteps.GroupSize * GroupId + 2;
}
public static class Group2
{
private const int GroupId = 2;
public const int Step1 = FlowSteps.GroupSize * GroupId + 1;
public const int Step2 = FlowSteps.GroupSize * GroupId + 2;
}
}
As you can see you have 100 steps available per group. If that isn't enough, just increase the group size. And your currentStep
is just an int
now.
Upvotes: 3