|
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.).
To iterate over ENUM values, you can use a loop combined with the size(enum_name, 2) function, which returns the number of names in the ENUM. This can be useful for validating input or applying logic across all possible states.
// Convert a text status into a numeric one mychoice$ = 'REJECTED' select case mychoice$ case nameof$(status->rejected) mystatus = status->rejected case nameof$(status->approved) mystatus = status->approved case nameof$(status->pending) mystatus = status->pending case else print "Unknown status: "; mychoice$ end select print 'status is ';mystatus|
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. |