How to create an enumeration of constants with and without explicit values in Pascal

1 Answer

0 votes
{ 
  Title: Enumeration of Constants in Free Pascal
  Example with and without explicit values
}

program EnumExample;

{$mode objfpc}  { Enables modern Object Pascal features }

uses
  SysUtils;

{ Enum WITHOUT explicit values }
type
  TColor = (Red, Green, Blue);  
  { Red = 0, Green = 1, Blue = 2 automatically }

{ Enum WITH explicit and mixed values }
type
  TStatus = (OK := 1, Warning := 5, Error, Critical := 10);
  { Error becomes 6 automatically }

var
  c: TColor;
  s: TStatus;

begin
  Writeln('Enum without explicit values:');
  Writeln('Red = ', Ord(Red));
  Writeln('Green = ', Ord(Green));
  Writeln('Blue = ', Ord(Blue));

  Writeln;
  Writeln('Enum with explicit and mixed values:');
  Writeln('OK = ', Ord(OK));
  Writeln('Warning = ', Ord(Warning));
  Writeln('Error = ', Ord(Error));
  Writeln('Critical = ', Ord(Critical));

  { Demonstrate usage }
  c := Green;
  if c = Green then
    Writeln('Color is Green');

  s := Error;
  if s = Error then
    Writeln('Status is Error');
end.



{ 
run:

Enum without explicit values:
Red = 0
Green = 1
Blue = 2

Enum with explicit and mixed values:
OK = 1
Warning = 5
Error = 6
Critical = 10
Color is Green
Status is Error

}

 



answered Apr 25 by avibootz

Related questions

...