在我的程序中,我将Enum划分为“Group”(为了清晰而重命名):
public enum FlowSteps
{
GROUP1_Step1,
GROUP1_Step2,
GROUP2_Step1,
GROUP1_Step2,
...
}
然后我的观点我检查当前的步骤:
FlowSteps currentStep = ...;
if (currentStep == GROUP1_Step1) { //Do something }
现在,我想重构一下我不太喜欢的这种分组,而且我的意思是:
public class FlowSteps
{
public enum GROUP1
{
Step1,
Step2
}
public enum GROUP2
{
Step1,
Step2
}
}
更清楚,但现在我无法比较我当前的步骤:
FlowSteps currentStep = ...;
if (currentStep == FlowSteps.GROUP1.Step1) { //Do something }
有关如何保持我的Enum这种分组并能够在不增加太多复杂性的情况下比较该类的任何建议吗?
你会怎么做?
答案 0 :(得分:3)
我认为枚举列表不是正确的方法。我会使用一组带有整数的类,使它们产生唯一的数字。
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;
}
}
如您所见,每组有100个步骤可用。如果这还不够,只需增加组大小即可。而您的currentStep
现在只是int
。
答案 1 :(得分:0)
SELECT *
FROM address
WHERE CONCAT(se10,ctry_nm) IN ('44444444USA');
然后可以根据它们的值进行比较,并且可以替换步骤的替代名称 - 就像上面的Group2.Step2一样。
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());
}
更进一步,如果需要枚举或序列化,可以将每个组放在Group1.Step1.Value == Group2.Step1.Value
的集合中。