Enumeration Types

Top  Previous  Next

At times, it may be useful for a programmer to define his own enumeration data types; for example, when working with the days of the week, it may be useful to refer to them by name in code, rather than by number:

 

 

dim i as integer

i = 2 ' i is an integer, and can only be assigned a numerical value. you would have to remember that 2 is Monday.

 

dim d as dayofweek

d = monday ' now d is a user-defined type, dayofweek, and can be assigned a value using the symbol Monday.

 

       

Enumeration definitions are made like this:

 

 

enum dayofweek

  sunday = 1,   ' if 1 is not specified, the default value associated with the first constant is 0.

  monday,       ' by default, increments the previous value by 1. can also be explicitly specified.

  tuesday,

  wednsday,

  thursday,

  friday,

  saturday,

  holiday = 99, ' as described above, values can be explicitly associated with any constant in the list.

  holiday2      'this will have the value of 100

end enum

 

An enumeration type would then be used within the code as shown above (d = Monday). Note that even though Monday is an identifier (i.e., an actual word, and not just some number) it does not have to be surrounded by quote marks (because it's not a string).

The value associated with each identifier within the enumeration type doesn't necessarily have to be unique; however, when you associate the same value with several constants, the distinction between these constants will be lost on compile time.

 

 

enum dayofweek

       Sunday = 1,

       Monday,    

       Tuesday,

       Wednsday,

       Thursday,

       Friday,

       Saturday,

       holiday = 99

       bestdayofweek = 7 ' bestdayofweek and Saturday are actually the same!

       fridaythethirteenth = -666 ' a negative constant.

end enum

 

 

Note that above, saturday was implicitly (automatically) associated with the value 7, and bestdayofweek was explicitly associated with the same value. Now, on compile-time, they would both be considered to be just the same.

Type Mapping for Enum Types

When the project is being compiled, all enumeration types are substituted with plain numerical values. the platform on which the code is running doesn't have to know anything about Saturday or about the best day of the week; for the platform, the number 7 is informative enough.

Hence, enumeration types are converted to various built-in numerical variable types. The actual numerical type used to store the enumeration type depends on the values associated with the constants within this enumeration type:

Values associated with constants do not exceed range

Actual variable type used to store enum type

-128 to 127

char

0 to 255

byte

-32768 to 32767

short

0 to 65535

word

-2147483648 to 2147483647

long

0 to 4294967295

dword

Notice, that enumeration types cannot be converted into real values, so you cannot use fractional numbers in you enums.

Some examples:

 

 

enum tiny

       tinyfoo,

       tinybar

end enum ' this enum will be associated with a char type

 

enum medium

       mediumfoo = 254

       mediumbar

end enum ' this enum will be associated with a byte type

 

enum impossible

       baddy = -1

       cruise = 4294967295

end enum ' this enum will raise a compile error, as no single variable type can hold its values

 

enum nofractions

       thisisok = 4294967295

       thisisnot = 125.25

end enum ' compiler won't accept this! Integer values only, please!

 

 

Enumeration types are helpful when debugging your code

For each enumeration type variable, the watch facility of the TIDE shows this variable's current numerical value with a correct identifier associated with this value. This usually proves to be very useful during debugging! After all, seeing "dayofweek= 2- Monday" is much less cryptic than just "dayofweek= 2"!