|
Enum Statement and Associated Built-in Functions |
The ENUM statement defines a related set of named
numeric constants. Values can be assigned explicitly or generated
automatically in sequence.
Enums are useful for representing states, choices, categories, command words, and other sets of related values. They improve readability by replacing unexplained numeric values with meaningful names.
In this syntax:
enum_namename1, name2, name3, ...You can also assign explicit numeric values if required:
When a member does not include an explicit value, Sheerpower assigns it the value of the preceding member plus 1. It is recommended that you do not use values of zero or duplicate values. This makes checking for unknown enum values easier.
In this example, the ENUM statement defines three
traffic-light states. Because no explicit values are supplied,
Sheerpower assigns the following values:
traffic_light->red is 1.traffic_light->yellow is 2.traffic_light->green is 3.
The current state is set to yellow. The
select case statement then selects the action associated
with that state.
The size(enum_name, 2) function returns the number of
members in an enum. In this example, it returns 3.
Using an enum improves readability by replacing hardcoded numeric values with meaningful names. It also reduces errors and makes the program easier to maintain.
Enum members can be assigned specific numeric values:
Members following an explicitly assigned value continue sequentially unless another explicit value is supplied:
Use meaningful and consistent member names. Keep each enum focused on one concept, such as states, categories, commands, or options.
Avoid unnecessarily long names that make expressions difficult to read. The enum type name should provide enough context to keep its member names concise.
Unless you have special case needs, there is no need to specify any values in your enum statement.
The enumname$() function converts an assigned enum value
into its corresponding member name. It is useful for error reporting,
logging, debugging, and displaying enum values.
enum_typeENUM statement.
valuename$
When a matching member is found, enumname$() returns its
name. If no member has the supplied value, it returns the value inside
question marks and sets _integer to zero.
Using enumname$() avoids duplicating enum member names as
string literals.
If a member is renamed, code that obtains the name through
enumname$() remains synchronized with the enum
declaration.
enumname$() to obtain the member name directly from
the enum.
enumname$() is called with a compile-time enum
member, such as
enumname$(status, status->approved), the resulting
string is calculated at compile time and embedded as a constant.
This requires no runtime lookup or allocation.
enumname$() in logs and messages to keep names
correct, consistent, and easy to maintain.
The enumvalue() function looks up an enum member by name
and returns the numeric value assigned to that member.
It is the inverse of enumname$():
The name comparison is case-insensitive. Therefore,
"yellow", "Yellow", and
"YELLOW" all match the same enum member.
enum_typeENUM statement.
name$resultEvery enum member has both an assigned numeric value and an ordinal position.
The assigned value is the number associated with the member. The ordinal is the member's position in the enum declaration, independent of its assigned value.
The first member has ordinal 1, the second has ordinal 2, and so on.
After enumvalue() performs a lookup,
_integer contains the ordinal position of the matching
member.
If no member matches, _integer is zero. Therefore, the
reliable not-found test is:
_integer, not the value returned by
enumvalue(). An enum member may legitimately have an
assigned value of zero.
The value returned by enumvalue() cannot reliably
indicate whether the lookup succeeded because zero may be a valid
enum value.
Both calls return zero. However, _integer distinguishes
the successful lookup from the failed lookup:
"off" matches the first member, so
_integer is 1.
"bogus" does not match a member, so
_integer is 0.
Using the ordinal guarantees that every successful lookup sets
_integer to a value greater than zero. Zero is reserved
exclusively for a failed lookup.
enumname$(), which places the matched
enum value in _integer. With
enumvalue(), the assigned value is already returned by
the function, so _integer provides the complementary
ordinal position.
An unknown name does not raise an exception. Instead,
enumvalue() returns zero and sets
_integer to zero.
A common use of enumvalue() is converting a text setting
into an enum value while detecting spelling errors or unsupported
values.
Use enumname$() and enumvalue() together
when enum member names are stored in a file, database, or
configuration setting.
A web form can submit an enum member name. The program can convert the name and substitute a default when the submitted name is not recognized.
The function can recognize enum member names while processing tokens or command words.
Unknown words are ignored because they set _integer to
zero.
The ordinal stored in _integer can be used as an index
into an array containing information associated with each enum
member.
In this example, value is 300 and
ordinal is 3.
| Operation |
enumvalue(enum_type, name$)
|
|---|---|
| Conversion | Member name to assigned numeric value |
| Name comparison | Case-insensitive |
| Successful lookup |
Returns the member's assigned value and sets
_integer to its ordinal position
|
| Failed lookup |
Returns zero and sets _integer to zero
|
| Not-found test |
if _integer = 0 then ...
|
| Inverse function |
enumname$(enum_type, value)
|
The two enum lookup functions are inverses of each other, and they
treat _integer differently. enumname$() places the
matched value in _integer; enumvalue()
places the matched ordinal position there. The table contrasts both
functions side by side.
| Aspect | enumname$(enum_type, value) |
enumvalue(enum_type, name$) |
|---|---|---|
| Conversion | Assigned value to member name | Member name to assigned value |
| Lookup key | Numeric value | Member name (string) |
| Returns on success | The member name, in uppercase | The member's assigned numeric value |
| Returns on failure | The supplied value wrapped in question marks, such as ?999? |
Zero |
_integer on success |
The matched enum value | The member's ordinal position (begins at 1) |
_integer on failure |
Zero | Zero |
| Reliable not-found test | Returned string has the ?value? form. You can also use if _integer = 0 then ...
if none of your enum values are zero. |
if _integer = 0 then ... — always reliable, since
ordinals begin at 1. |
| Name comparison | Not applicable | Case-insensitive |
| Compile-time constants folding | Yes | Yes |
| Inverse function | enumvalue() |
enumname$() |
|
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. |