1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
Enum namespace
System
Declaration
Enum = class abstract(System.Object, IComparable, IFormattable, IConvertible)
Description
Provides the base class for enumerations.
Example
program System_Enum;

{$APPTYPE CONSOLE}
{$AUTOBOX ON}

uses System.Collections;

type
  SamplesEnumTest = class
  type
    Days = (Saturday, Sunday, Monday, Tuesday,
            Wednesday, Thursday, Friday);
            
    BoilingPoints = (Celcius = 100, 
                     Fahrenheit = 212);
    
    [FlagsAttribute]
    Colors = (Red = 1, 
              Green = 2, 
              Blue = 4, 
              Yellow = 8);
  public
    class procedure Main;
  end;

{ EnumTest }

class procedure SamplesEnumTest.Main;
var
  myColors: Colors;
  myEnumerator: IEnumerator;
  s: string;

  boiling: System.Type;
  weekdays: System.Type;
begin
  weekdays := TypeOf(Days);
  boiling := TypeOf(BoilingPoints);

  Console.WriteLine('The days of the week, and 
                     their corresponding values 
                     in the Days Enum are:');
  myEnumerator := System.Array(
                  Enum.GetNames(weekdays)).GetEnumerator;
  myEnumerator.Reset;
  while myEnumerator.MoveNext do
  begin
    s := myEnumerator.Current.ToString;
    Console.WriteLine('{0,-11}= {1}', 
                      s, 
                      Enum.Format(weekdays, 
                                  Enum.Parse(weekdays, s), 
                                  'd'));
  end;

  Console.WriteLine;
  Console.WriteLine('Enums can also be created
                     which have values that represent 
                     some meaningful amount.');
  Console.WriteLine('The BoilingPoints Enum defines 
                     the following items, and 
                     corresponding values:');

  myEnumerator := System.Array(
                  Enum.GetNames(boiling)).GetEnumerator;
  myEnumerator.Reset;
  while myEnumerator.MoveNext do
  begin
    s := myEnumerator.Current.ToString;
    Console.WriteLine('{0,-11}= {1}', 
                      s, 
                      Enum.Format(boiling, 
                                  Enum.Parse(boiling, s), 
                      'd'));
  end;

  myColors := (Colors.Red or Colors.Blue or Colors.Yellow);
  Console.WriteLine;
  Console.WriteLine('myColors holds a 
                     combination of colors. Namely: {0}', 
                    myColors);
end;

//program test
begin
  SamplesEnumTest.Main;
end.
Example output
The days of the week, and their 
corresponding values in the Days Enum are:
Saturday   = 0
Sunday     = 1
Monday     = 2
Tuesday    = 3
Wednesday  = 4
Thursday   = 5
Friday     = 6

Enums can also be created which have values 
that represent some meaningful amount.

The BoilingPoints Enum defines the 
following items, and corresponding values:
Celcius    = 100
Fahrenheit = 212

myColors holds a combination of 
colors. Namely: Red, Blue, Yellow

Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are searching for!

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.