问题描述
我正在C#中的一个项目上工作,由于某些原因,当我尝试为一个枚举变量分配一个值时,这个赋值不会发生。我会复制我的代码,但这只是一个简单的任务。这就像: testVar = MyEnum.TYPE_OF_ENUM;
其中 testVar
的类型为 MyEnum
。当我使用VisualStudio调试器浏览此行时,我可以看到 testVar
的值不会更改。什么可能导致作业失败?
编辑:
确定我将提供更多的上下文。 / p>
public enum MyEnum1
{
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT
}
public enum MyEnum2
{
A,
B,
C,
D,
E,
F
}
public void StateMachine (MyEnum1 state1)
{
if(state2 == MyEnum2.A)
{
switch(state1)
{
case MyEnum1.ONE:
state2 = MyEnum2.B;
MyFunc(MyEnum2.B);
break;
默认值:
break;
}
}
else if(state2 == MyEnum2.B)
{
switch(state1)
{
case MyEnum1.ONE :
state2 = MyEnum2.B;
MyFunc(MyEnum2.B);
break;
case MyEnum1.THREE:
state2 = MyEnum2.C;
MyFunc(MyEnum2.C);
break;
默认值:
break;
}
}
// Etcetera
}
失败发生在 state2 =任何
作业。 (state2是一个字段而不是属性)
MyEnum.TYPE_OF_ENUM是枚举中的第一个值,还是分配了TYPE_OF_ENUM到一个整数值0?
未初始化的枚举将被设置为0,因此可能已经匹配。
否则,你还有其他一些你还没有注意到的东西。
I am working on a project in C#, and for some reason, when I try to assign a value to an enum variable, the assignment does not happen. I would copy my code, but it is really just a simple assignment. It's something like:
testVar = MyEnum.TYPE_OF_ENUM;
where testVar
is of type MyEnum
. When I step through this line using the VisualStudio debugger, I can see that the value of testVar
does not change. What could possibly make an assignment fail like that?
EDIT:
Ok I will provide more context.
public enum MyEnum1
{
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT
}
public enum MyEnum2
{
A,
B,
C,
D,
E,
F
}
public void StateMachine(MyEnum1 state1)
{
if(state2 == MyEnum2.A)
{
switch (state1)
{
case MyEnum1.ONE:
state2 = MyEnum2.B;
MyFunc(MyEnum2.B);
break;
default:
break;
}
}
else if (state2 == MyEnum2.B)
{
switch(state1)
{
case MyEnum1.ONE:
state2 = MyEnum2.B;
MyFunc(MyEnum2.B);
break;
case MyEnum1.THREE:
state2 = MyEnum2.C;
MyFunc(MyEnum2.C);
break;
default:
break;
}
}
// Etcetera
}
The failure occurs on the state2 = whatever
assignments. (state2 is a field, not a property)
Is MyEnum.TYPE_OF_ENUM the first value in the enum, or is TYPE_OF_ENUM assigned to a integer value of 0?
A non-initialized enum will be set to 0, so it may already match.
Otherwise, it is much more likely that you have something else going on that you haven't noticed yet.
这篇关于什么可能导致作业不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!