[英]Test C# Enum Flags on Multiple Conditions
I have a test program. 我有一个测试程序。 I want to use an
enum
with flags to test multiple conditions but i get the wrong result. 我想使用带有标志的
enum
来测试多个条件,但是得到错误的结果。
This is my enum : 这是我的枚举:
[Flags]
enum CoverOptionTypes
{
MainLife = 0x0001,
PolicyPayerMainLife = 0x0002,
Spouse = 0x0004,
Child = 0x0008,
AdditionalChildren = 0xF
}
When i create the enum and assign values, i dont seem to get the expected values. 当我创建枚举并分配值时,我似乎没有得到期望的值。
If i do this 如果我这样做
var option = CoverOptionTypes.MainLife;
and test with the following : 并测试以下内容:
if (option.HasFlag(CoverOptionTypes.MainLife)
{
Console.WriteLine("Main + PP Screen + Parents + Ext Family");
}
I get the correct output. 我得到正确的输出。 When i try multiple scenarios as follows :
当我尝试以下多种情况时:
var option = CoverOptionTypes.MainLife;
option |= CoverOptionTypes.PolicyPayerMainLife;
option |= CoverOptionTypes.Child;
option |= CoverOptionTypes.AdditionalChildren;
if (option.HasFlag(CoverOptionTypes.MainLife) && (
option.HasFlag(CoverOptionTypes.PolicyPayerMainLife) &&
!option.HasFlag(CoverOptionTypes.Spouse) &&
!option.HasFlag(CoverOptionTypes.Child) &&
!option.HasFlag(CoverOptionTypes.AdditionalChildren)))
{
Console.WriteLine("Main + PP Screen + Parents + Ext Family");
}
else if (option.HasFlag(CoverOptionTypes.MainLife) &&(
option.HasFlag(CoverOptionTypes.PolicyPayerMainLife) &&
option.HasFlag(CoverOptionTypes.Spouse) &&
!option.HasFlag(CoverOptionTypes.Child) &&
!option.HasFlag(CoverOptionTypes.AdditionalChildren)))
{
Console.WriteLine("Main + Spouse + PP Screen + Parents + Ext Family");
}
else if (option.HasFlag(CoverOptionTypes.MainLife) && (
option.HasFlag(CoverOptionTypes.PolicyPayerMainLife) &&
option.HasFlag(CoverOptionTypes.Spouse) &&
option.HasFlag(CoverOptionTypes.Child) &&
!option.HasFlag(CoverOptionTypes.AdditionalChildren)))
{
Console.WriteLine("Main + Spouse + Child + PP Screen + Parents + Ext Family");
}
else if (option.HasFlag(CoverOptionTypes.MainLife) && (
option.HasFlag(CoverOptionTypes.PolicyPayerMainLife) &&
option.HasFlag(CoverOptionTypes.Spouse) &&
option.HasFlag(CoverOptionTypes.Child) &&
option.HasFlag(CoverOptionTypes.AdditionalChildren)))
{
Console.WriteLine("Main + Spouse + Child + Additional Children + PP Screen + Parents + Ext Family");
}
else if (option.HasFlag(CoverOptionTypes.MainLife) && (
option.HasFlag(CoverOptionTypes.PolicyPayerMainLife) &&
!option.HasFlag(CoverOptionTypes.Spouse) &&
option.HasFlag(CoverOptionTypes.Child) &&
!option.HasFlag(CoverOptionTypes.AdditionalChildren)))
{
Console.WriteLine("Main + Child + PP Screen + Parents + Ext Family");
}
else if (option.HasFlag(CoverOptionTypes.MainLife) && (
option.HasFlag(CoverOptionTypes.PolicyPayerMainLife) &&
!option.HasFlag(CoverOptionTypes.Spouse) &&
option.HasFlag(CoverOptionTypes.Child) &&
option.HasFlag(CoverOptionTypes.AdditionalChildren)))
{
Console.WriteLine("Main + Child + Additional Chidren + PP Screen + Parents + Ext Family");
}
I get this output 我得到这个输出
Main + Spouse + Child + Additional Children + PP Screen + Parents + Ext Family
The output is wrong as i do not have the Spouse
flag turned on. 输出错误,因为我没有打开“
Spouse
标志。 When i debug, i can see the flags set but the if decision goes into the wrong if
statement. 当我调试时,我可以看到设置的标志,但是if决策进入错误的
if
语句。
Pretty sure this is because of the integer values you chose for your enum. 可以肯定的是,这是因为您为枚举选择了整数值。
Consider the following: 考虑以下:
HasFlag
formula is (int)EnumValue & (int)TargetValue = (int)TargetValue
(source: https://msdn.microsoft.com/en-us/library/system.enum.hasflag.aspx?f=255&MSPPError=-2147217396 ) HasFlag
公式为(int)EnumValue & (int)TargetValue = (int)TargetValue
(来源: https : (int)EnumValue & (int)TargetValue = (int)TargetValue
)
Using this, we can see that 使用这个,我们可以看到
0xF & 0x0004
returns 0x0004
0xF & 0x0004
返回0x0004
Which basically means that when AdditionalChildren
is set, Spouse
is also set. 基本上,这意味着当设置了
AdditionalChildren
时,还将设置了Spouse
。
Also, your MainLife
and PolicyPayerMainLife
have the same values. 另外,您的
MainLife
和PolicyPayerMainLife
具有相同的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.