Enum type

From Free Pascal wiki
Revision as of 13:05, 14 February 2020 by Trev (talk | contribs) (→‎Memory requirement: Added table)
Jump to navigationJump to search

English (en)


Back to data types.


Range

  • with 0 .. 255 characters, the data type is automatically Byte
  • with 255 .. 65,535 characters, the data type is automatically Word
  • over 65,535 characters, the data type is automatically Longword

Memory requirement

This depends on the data type and the number of elements. By default, an enumeration is stored in a Longword and requires 4 bytes. Depending on if a switch is used, {$PACKENUM x} (abbreviated {$Zx}) and the value range of the data type, it can be adjusted.

Memory requirements
Maximum value range <= 255 <= 65,535 > 65,535
No switch or {$PACKENUM DEFAULT}
or {$PACKENUM 4} or {$Z4}
4 4 4
{$PACKENUM 2} or {$Z2} 2 2 4
{$PACKENUM 1} or {$Z1} 1 2 4

Properties

The data type Enum Type is an enumeration of unsigned constants.

In addition, the unsigned integer data type to be used can be specified for the data type using a switch.

  • at 0 .. 255 characters the switch for the byte data type:
    • {$Z1} or {$ PACKENUM 1}
  • at 255 .. 65535 characters the switch for the Word data type:
    • {$Z2} or {$ PACKENUM 2}
  • at 65535 .. 4294967295 characters the switch for the LongWord data type:
    • {$Z4} or {$ PACKENUM 4}

The values ​​of the enumeration always increase by 1 to the next element.

Examples

Examples of declaring the Enum data type

The values ​​of the enumeration start at 0 by default:

  Type
   {$PACKENUM 2}
   LargeEnum = (zero, one, two);

By default, the values ​​of the enumeration start at 0, unless the enumeration is assigned a different starting value. Then the count starts with the assigned initial value.

  Type
   {$PACKENUM 4}
   SmallEnum = (Six := 6, Seven, Eight);

If a higher value is assigned to an element in the middle of the enumeration, counting continues from this value.

  Type
   {$ PACKENUM 1}
   MiddleEnum = (zero, ten := 10, eleven, twelve);

Explicit use of the byte data type for the enumeration.

  Type
   byte = (zero, one, two);

Examples of assigning the Enum data type

 var 
   S : SmallEnum;
   M : MiddleEnum;
   L : LargeEnum;

Examples for the conversion of the data type Enum

  Application.MessageBox(PChar(IntToStr(Qword(Ord(Six)))), 'Conversion' , MB_OK);
  Application.MessageBox(PChar(IntToStr(Qword(Ord(p.Six)))), 'Conversion' , MB_OK);