59 of 172
ZBasic Language Reference 52 ZX Microcontroller Family 3.2 Enumerations In some situations it is convenient to be able to refer to the values of a variable by a name rather than by a numeric value. An enumeration type essentially allows you to define a new data type and name the set of values for that type. The syntax for defining an enumeration is: [Public | Private] Enum <name> <member-name> [= <constant-expr>] ... End Enum In this syntax, <member-name> is an identifier that names a member of the enumeration. The optional <constant-expr> represents a value that you want to be associated with that member name. For any member that does not have an explicit member value specified, a member value will be automatically assigned that is one larger than the preceding member or zero for the first member. The ellipsis in the syntax above indicates that there may be zero or more additional member definitions. Member names must be unique within the set of members for each enumeration. A particular member name may, however, be used in multiple enumerations. See the discussion below for information about how ambiguity is resolved. If neither Private nor Public is specified on an enumeration definition, the enumeration is public. An enumeration may be defined at the module level or it may be defined within a subroutine or function, either at the outer level or within any inner block. In the latter case, the Public and Private keywords have no useful purpose and are therefore not allowed. Examples Enum Pet Dog Bird Snake End Enum In this case, the members will be assigned values of 0, 1 and 2 respectively. Enum Mammal Cat Dog Elephant = 5 Horse End Enum Here the members will have the values 0, 1, 5 and 6 respectively. Note that if explicit values are specified, they must be larger than the value assigned, explicitly or implicitly, to the preceding member. The value associated with enumeration members is unsigned. After an enumeration has been defined, the enumeration name may be used as a <type> in a variable, constant or structure definition. Enumerations may also be used as the <type> in the formal parameter list of a subroutine or function definition. Note, however, that a Public subroutine or function cannot be defined with a parameter that is a Private Enum. Example Public animal as Mammal animal = Elephant animal = Mammal.Dog