Enum as Bitwise flags

Is there really no way to use Enum as bitwise flags others than doing cumbersome forced conversions to and from integer?

Like this works:
Dim a as Test = Test(Integer(Test.x) or Integer(Test.y))

While this gets rejected by the compiler:
Dim a as Test = Test.x or Test.y

(Test is Integer Enum that Defines x = 1 and y = 2)

Ctype maybe usefull

CType would just do same as my Casts are doing in the upper code that works, but also keep the code equally dirty as the upper case is.

// Works but is ugly
Dim a as Test = Test(Integer(Test.x) or Integer(Test.y))

// Works but is even uglier
Dim a as Test = Ctype(CType(Test.x,Integer) or CType(Test.y,Integer),Test)

// Compiler rejects:
Dim a as Test = Test.x or Test.y

Short answer: No

I’ve wrote a class in the past to handle bit flags as natively Xojo doesn’t have a better way. https://forum.xojo.com/48597-flags64-class-storing-64-flags-in-one-int64

Probably would be best if Xojo had a flag on the enum to specify it as “Flags” (could be attribute), and then let the compiler say the Or is ok when it is flags.

(Similar how C# does it they just put Attribute on it to tell the compiler its flags, keeping the Enum strict in other cases)

@Björn Eiríksson — I have mostly stopped using enum for that one reason. The way they are implemented in Xojo make them a pain in the arschloch :slight_smile:

Well let us hope they are listening. There is no need for Xojo to be bad for no reason when it is small thing like this that at same time makes it so bad. !

@Björn Eiríksson — Unfortunately, that is exactly the kind of things that will never get fixed even with a proper bug report.

[quote=425792:@Björn Eiríksson]Is there really no way to use Enum as bitwise flags others than doing cumbersome forced conversions to and from integer?

Like this works:
Dim a as Test = Test(Integer(Test.x) or Integer(Test.y))

While this gets rejected by the compiler:
Dim a as Test = Test.x or Test.y

(Test is Integer Enum that Defines x = 1 and y = 2)[/quote]

If Test defines X and Y then this line results in a value that is not one of the enumerated values

Dim a As Test = Test.x Or Test.y

so the result should be an integer - not a “Test”

Enums simply don’t have several operators defined Or, And, Xor, addRight AddLeft, etc.

[quote=425820:@Jason Parsley]If Test defines X and Y then this line results in a value that is not one of the enumerated values

Dim a As Test = Test.x Or Test.y

so the result should be an integer - not a “Test”

Enums simply don’t have several operators defined Or, And, Xor, addRight AddLeft, etc.[/quote]

Which is why other languages have attribute to put on Enums if its “Flags” to use for bitwise operations. Keeping the enum strict if its not Flags.

Would a Class with the enum values defined as shared methods work for you?

Something like:

[code]Class myEnum
Shared Function Two() as Integer
Return 2
End Function

Shared Function Four() as Integer
Return 4
End Function
End Class[/code]

You could then write code like this:

Dim n as Integer = myEnum.Two or myEnum.Four

n would be 6.

I would say…not since thats basically same as doing Constants on a class.

But with constants you loose the typedefinition which you want to be still strongly typed even if you can and will bitmask it, so the signature of a method that takes in the Bitmask is something as myEnum and not something as Integer. That is the whole point of flags and bitmasks.

I just realized that Enums do have > and < operators which is handy (I had been casting them to integers but that isn’t needed).

I agree that allowing OR or perhaps plus operations would be useful.