我有一个包含需要设置和清除位字段值的函数的类。代码如下。在行
FlagsValues &= ~(UInt16)EFlagsValues.NIn;
下面我得到的错误是“常数值”-1025'无法转换为ushort
我的代码有什么问题,为什么我会收到此错误。?
public class MySetupC
{
private UInt16 m_FlagsValues;
private UInt16 m_FlagsMask;
public UInt16 FlagsMask { get { return m_FlagsMask; } private set { m_FlagsMask = value; } }
public UInt16 FlagsValues { get { return m_FlagsValues; } private set { m_FlagsValues = value; } }
[Flags]
public enum EFlagsMask
{
None = 0,
AIn = (1 << 0), //1
BIn = (1 << 1), //2
CIn = (1 << 2), //4
DIn = (1 << 3), //8
EIn = (1 << 4), //16
FIn = (1 << 5), //32
JIn = (1 << 6),
KIn = (1 << 7),
LIn = (1 << 8),
MIn = (1 << 9),
NIn = (1 << 10),
OIn = (1 << 11),
}
[Flags]
public enum EFlagsMask
{
None = 0,
AIn = (1 << 0), //1
BIn = (1 << 1), //2
CIn = (1 << 2), //4
DIn = (1 << 3), //8
EIn = (1 << 4), //16
FIn = (1 << 5), //32
JIn = (1 << 6),
KIn = (1 << 7),
LIn = (1 << 8),
MIn = (1 << 9),
NIn = (1 << 10),
OIn = (1 << 11),
}
public void SetNIn(bool enable)
{
if (enable)
{
FlagsValues |= (UInt16)EFlagsValues.NIn;
FlagsMask |= (UInt16)EFlagsMask.NIn;
}
else
{
//negate
FlagsValues &= ~(UInt16)EFlagsValues.NIn;
FlagsMask |= (UInt16)EFlagsMask.NIn;
}
}
}
答案 0 :(得分:1)
从UInt16中获取你的枚举
public enum EFlagsMask : UInt16 {
然后,对枚举值执行〜运算符,而不是UInt16;
FlagsValues &= (UInt16)~EFlagsMask.NIn;