Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an enum with explicit values in Delphi 5

Tags:

enums

delphi

Currently I am recompiling a higher version of Delphi (XE) project to lower version 5.

I have multiple enums with explicit values as below:

type 
  TSize = (ftSmallSize=10, ftMediumSize=15, ftLargeSize=20, ftExtralarge=24)

When I compile this code, an error occurs with the message:

',' or ')' expected but '=' found

How can I recompile this code in Delphi 5?

like image 403
user3733328 Avatar asked Sep 11 '25 17:09

user3733328


2 Answers

Yes, the option to specify fixed (ordinal) values in an enumeration was added in Delphi 6.

Probably the simplest workaround is to do

type
  TSize = Byte;

const
  ftSmallSize = 10;
  ftMediumSize = 15;
  ftExtraLarge = 24;

Obviously, you lose some type safety, but it will (likely) compile without any further changes.

Just make sure to choose an integer type of the right size when you define TSize. You should make it the same as SizeOf(TSize) in your original code.

like image 127
Andreas Rejbrand Avatar answered Sep 15 '25 23:09

Andreas Rejbrand


You can decouple the values from the enum type:

type
  TSize = (ftSmallSize, ftMediumSize, ftLargeSize, ftExtralarge);
const
  cSizeValues: array[TSize] of Integer = (10, 15, 20, 24);

Or define a function GetSizeValue(ASize: TSize) with a case ASize of - that would be more flexible.

Use like it this:

 Edit1.Height := cSizeValues[ftMediumSize];

or

 Edit1.Height := GetSizeValue(ftMediumSize);
like image 39
Uli Gerhardt Avatar answered Sep 16 '25 00:09

Uli Gerhardt