Arrays with enumerations

Top 

Example code : Defining an array size using enumerations

type

   TCars  = (Ford, Vauxhall, GM, Nissan, Toyota, Honda);

 

procedure TForm1.W3Button2Click(Sender: TObject);

var

   cars    : array[TCars] of string;          // Range is 0..5

   japCars : array[Nissan..Honda] of string;  // Range is 3..5

begin

   // We must use the appropriate enumeration value to index our arrays:

   japCars[Nissan] := 'Bluebird';   // Allowed

   japCars[Toyota] := 'Galaxy';     // Allowed

// japCars[4]      := 'Corolla';    // Not allowed

// japCars[Ford]   := 'Galaxy';     // Not allowed

 

WriteLn(japCars[Nissan]);

WriteLn(japCars[Toyota]);

end;

Result is:

------------------------

Bluebird

Galaxy

------------------------

Note: The cars array is defined using just a data type - TCars. The size and range of the data type dictates how we use the array.