ENUM statement

Purpose: ENUM defines a list of variables which represent integer numbers and provides for enumeration of the variables. ENUM is similar to BCX CONST.


Syntax 1:

 ENUM Variable1%, Variable2%, Variable3%, ...

 Parameters:

  • Variable1% will be enumerated a value of 1
  • Variable2% will be enumerated a value of 2
  • Variable3% will be enumerated a value of 3

Syntax 2:

 ENUM Variable1% = 3, Variable2%, Variable3%, ...

 Parameters:

  • Variable1% = 3 will be enumerated a value of 3
  • Variable2% will be enumerated a value of 4
  • Variable3% will be enumerated a value of 5

ENUM can also be structured as an ENUM...END ENUM block. ENUM defined in this way is always GLOBAL in scope.


Syntax 3:

 ENUM
  Variable1% = 3
  Variable2%
  Variable3%
  ...
 END ENUM

 Parameters:

  • Variable1% = 3 will be enumerated a value of 3
  • Variable2% will be enumerated a value of 4
  • Variable3% will be enumerated a value of 5

Instead of coding :


 CONST Mon = 1
 CONST Tue = 2
 CONST Wed = 3
 CONST Thu = 4
 CONST Fri = 5
 CONST Sat = 6
 CONST Sun = 7

ENUM could be used.


 ENUM Mon=1, Tue, Wed, Thu, Fri, Sat, Sun

BCX's implementation of ENUM places the declarations inside the SUB or FUNCTION within which they are declared. If declared in a console app outside any user defined SUB or FUNCTION then the ENUM is placed in the main() function.

Consider the following valid sample:


 ENUM Mon=1, Tue, Wed, Thu, Fri, Sat, Sun   'declared and visible in main()
 
 SUB foo1
  ENUM Mon=10, Tue, Wed, Thu, Fri, Sat, Sun
 END SUB

 SUB foo2
  ENUM Mon=20, Tue, Wed, Thu, Fri, Sat, Sun
 END SUB

In a console application, if a global ENUM is needed, the above restriction regarding scope can be circumvented by using the ENUM...END ENUM block syntax which is always GLOBAL in scope. A second method for global scope is to use the $NOMAIN directive and a SUB main() as in this example.


 $NOMAIN

 ENUM Mon=1, Tue, Wed, Thu, Fri Sat, Sun

 SUB main()
  PRINT Wed
  KEYPRESS
 END SUB

The program below demonstrates beginning a series with the base number 100 then changing the base to 200.

Example:


 ENUM a=100, b, c, d=200, e, f

 PRINT a,b,c,d,e,f

Result: 100 101 102 200 201 202

Example: ENUM...END ENUM block example.


 ENUM
  apples
  bananas
  oranges
 END ENUM

 PRINT apples
 PRINT bananas
 PRINT oranges
 PRINT

 ENUM
  grapes = 100  'start at 100, then increment by 1
  berries
  kiwis
 END ENUM

 PRINT grapes
 PRINT berries
 PRINT kiwis