New Enumeration Enhancement: Using the enumeration as a Constructor which takes an Integer

In Greg O’Lone’s recent blog posting, he discusses the new enumeration enhancements in Xojo 2021r2. One of the enhancements is the ability to use the enumeration as a Constructor which takes an Integer.

I have attempted to duplicate this new feature with the following code:

Enum SomeEnum // placed inside a Module
Foo = 0
Bar = 1
Baz = 2

Var x as New SomeEnum(2) // Using the enumeration as a Constructor which takes an Integer

If x = SomeEnum.Baz then
// x = SomeEnum.Baz
End if

But I get the following error: “Cannot create an instance of enum Module1.SomeEnum with New because it is not a class.
Var x as New SomeEnum(2)”.

I am using the new Xojo version Xojo 2021r2.
I am sure I am doing something wrong here. Can anyone help?

I just tried this myself thinking that it only applied to the new Binary enums, but I get an error that the constructor is protected when I try that. I have a message in to @Greg_O_Lone

Public Enum SomeEnum
One
Two
Three
End Enum
var x as new SomeEnum(2)

OK, it looks like the addition of a constructor in the blog post was an error. The constructor is not exposed, so you would use something like the following to cast an integer to an enum value:

var x as SomeEnum = 2

When I try that I get the following error " Type mismatch error. Expected enum Module1.SomeEnum, but got Int32".

Interesting. I don’t see that. Is your enum scope set to Global? Does it say “Xojo 2021 Release 2” in the bottom-right of the Xojo window?

Ah, is your enum set to Binary in the inspector? If I turn off that property then I receive that message.

If you’re not using a Binary enum, then you need to cast:

var x as SomeEnum = SomeEnum(2)

You guys saw my blog post: How binary enumeration is implemented in Xojo

Binary Enums are classes and sadly the constructor is private.
Normal enums are just integers with some compiler handling.

Yes! That’s the answer! I did not have the enum set to Binary. Thank you!

Happy to help! Be sure to mark a Solution so future users who find this via search can go right to it.

FWIW, you don’t need to cast it. This will work too:

Var x as SomeEnum = 2

John got something like this using that:

Binary off, shows that. Binary on, no error.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.