|
Enum Statement |
The ENUM statement in Sheerpower allows you to define a set of related constants with automatically assigned sequential numbers. This is useful for creating state machines, categorizing options, or any situation where you need a clear, consistent, and easily maintainable set of named values.
In this syntax:
In this example, the ENUM statement defines three states for a traffic light, with red as 1, yellow as 2, and green as 3. The current state is set to yellow, and a Select Case statement determines the action to take based on the traffic light's current state.
The size(enum_name, 2) function returns the number of fields in the ENUM, making it easy to manage and iterate over the defined values.
Using ENUM improves code readability by replacing hardcoded numbers with meaningful names, reducing errors and making maintenance easier.
When defining an ENUM, use meaningful and consistent names for your values to make the code more readable. Avoid long names that could clutter the code, and try to keep the ENUMs simple and focused on one concept (e.g., states, categories, etc.).
Be aware that ENUM values in Sheerpower are sequential, starting from 1. If your application requires non-sequential values, consider using a cluster with manually assigned values instead. Also, make sure to handle cases where an invalid ENUM value might be used by incorporating default cases in your Select Case statements.
To iterate over ENUM values, you can use a loop combined with the size(enum_name, 2) function, which returns the number of fields in the ENUM. This can be useful for validating input or applying logic across all possible states.
The NAMEOF$() function is especially handy with
ENUM values in Select Case statements for error
reporting, logging, and debugging. It eliminates hardcoded strings and
keeps messages in sync with the actual enum identifiers.
NAMEOF$() to derive the display
name directly from the enum member you’re switching on.status->approved), the resulting string
from NAMEOF$() is calculated at compile time and
embedded as a constant. That means no runtime cost—no lookups,
no tables, no allocations. With flag=true, you can log
fully qualified names (e.g., STATUS->APPROVED) with
the same zero-cost behavior in these cases.NAMEOF$() in
Select Case branches and logs to keep messages correct,
consistent, and maintenance-free, without adding runtime overhead.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |