From Xojo Documentation(Redirected from Enum)
An enumeration (or enum) is a data type that consists of a group of named values (called elements). By default, the elements are numbered consecutively, starting with zero, but you can assign any Integer value to an element.
Enumerations are added to classes (and modules) using Insert ↠ Enumeration. From the Enum Editor, you can add the named values. In your code, you refer to an enum by its name and refer to its elements by using dot notation. For example:
So why should you use an Enum instead of an Integer constant? An Integer constant is a great way to refer to a constant value using a name. But since it is equivalent to an Integer, you cannot restrict its usage. With an enum, you can enforce that only the specific set of values are allowed.
Although Enumerations only accept Integer constants, the enumeration values cannot be treated as Integers in your code because an Enumeration is a separate type. Likewise, Integer values cannot be assigned to an enumeration. Should you need to get the Integer value of an enumeration, you need to cast it to an integer.
You refer to an Enum in your code using dot notation. If the Enum is in a module and has Global scope it is referred to as:
If it is in a module and has Public scope, it uses the module name in the notation:
If it is in a class then you have to use the full notation irrespective of the scope:
Passing As Parameters
An enumeration can be used to restrict the values that can be passed (for example to a method) to only those defined in the enumeration. This is done by defining the passed parameters as of type enumeration.
So if you have a method declared as
and you do
any range checks have to be in your code for the method and then you have to raise an error or do "something intelligent" to signal the out of bounds value
If however you have
Foo = 0
Bar = 1
Baz = 2
Sub Test(x As SomeEnum) // <<<<<<<<<< passing the enum instead of just an integer
Test(123) // This wont compile !!!!
then the compiler has, at compile time, detected the error in your code.
Consider a global Enumeration named SecurityLevel that has four elements: Unauthorized, Minimal, Maximum, and Forced. Their Integer values range from 0 to 3.
To assign a level of Minimal to a variable of type SecurityLevel:
The level variable can only be assigned Enum elements. You cannot just assign its Integer value:
You can, however, get the Integer value of an element by casting it to an Integer:
To pass an enumeration to a method, use its type name:
If level = SecurityLevel.Minimal Then
// Do something