What is the difference between the code bellow
' no Flags'
Public Enum MyEnum
Monday = 1
Tuesday = 2
Wednesday = 4
Thursday = 8
End Enum
and
<Flags()> _
Public Enum MyEnum
Monday = 1
Tuesday = 2
Wednesday = 4
Thursday = 8
End Enum
I do the
Dim days As MyEnum = MyEnum.Monday Or MyEnum.Tuesday Or MyEnum.Wednesday
If (days And MyEnum.Tuesday) = MyEnum.Tuesday Then
Console.WriteLine("Tuesday OK") ' here'
Else
Console.WriteLine("Tuesday NOK")
End If
If (days And MyEnum.Thursday ) = MyEnum.Thursday Then
Console.WriteLine("Thursday OK")
Else
Console.WriteLine("Thursday NOK") ' here'
End If
and obtain the same result in both cases(with or without FlagAttribute).
Basically, it tells the CLR that the values of the enum can be combined. Without this attribute, combining the values would result in an unknown value (but it would still be valid). With the attribute, the combination is correctly interpreted
Without the Flags attributes :
' Gives "Monday, Tuesday" '
Dim s As String = (MyEnum.Monday Or MyEnum.Tuesday).ToString()
Without the Flags attributes :
' Gives "3" '
Dim s As String = (MyEnum.Monday Or MyEnum.Tuesday).ToString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With