什么可能导致作业无法完成?
我正在使用 C# 开发一个项目,由于某种原因,当我尝试为枚举变量赋值时,分配不会发生。我会复制我的代码,但这实际上只是一个简单的作业。它类似于:
testVar = MyEnum.TYPE_OF_ENUM;
其中 testVar
的类型为 MyEnum
。当我使用 VisualStudio 调试器单步执行此行时,我可以看到 testVar
的值没有改变。什么可能导致任务失败?
编辑:
好的,我将提供更多背景信息。
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
}
失败发生在 state2 = another
分配上。 (state2是一个字段,而不是一个属性)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能性是设置器损坏的财产:
One possibility is property with a broken setter:
如果您的枚举有多个具有相同基础值的值,则它可能看起来不会改变:
在这里,您只会看到
One
。类似地,标志枚举如下:您永远不会看到
All
,只有One |两个。
If your enum has multiple values with the same underlying value, it may appear to not change:
Here, you'd only ever see
One
. Similarly, a flag enumeration like:You'd never see
All
, justOne | Two
.MyEnum.TYPE_OF_ENUM 是枚举中的第一个值,还是 TYPE_OF_ENUM 分配给整数值 0?
未初始化的枚举将被设置为 0,因此它可能已经匹配。
否则,很可能您还没有注意到正在发生其他事情。
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.