3

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user3733328
  • 133
  • 1
  • 7
  • 2
    I tried this in Delphi 4, and indeed it is considered a syntax error. A workaround is this: `type TSize = Byte; const ftSmallSize = 10; ftMediumSize = 15; ftExtraLarge = 24;` – Andreas Rejbrand Aug 09 '21 at 12:31

2 Answers2

4

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.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    Trivia: I never even tried to declare enum types with fixed values but instead declared constants right away in D7. Good to know I can. – AmigoJack Aug 09 '21 at 13:02
  • That this feature, that specific values can be added to enumerators, was added in Delphi 6 is the exact information I was searching for, hard to google but `',' or ')' expected but '=' found [delphi]` on SO did it. – Wolf Oct 08 '21 at 09:10
3

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);
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • 1
    In general, you also need a function that performs a simple linear search for the inverse transformation. (And of course, with this solution you still have full type safety, but a lot of code may require tiny adjustments: `ftMediumSize -> cSizeValues[ftMediumSize]`.) – Andreas Rejbrand Aug 09 '21 at 18:17
  • 1
    @AndreasRejbrand: You're right, for existing code to downgrade it's probably not worth the hassle. For new types I'd prefer it over enum types with explicit values as well as constants. – Uli Gerhardt Aug 09 '21 at 19:46
  • If access is a general standard call, then one can consider overriding the operators. [http://docwiki.embarcadero.com/RADStudio/XE/en/Operator_Overloading] – USauter Aug 16 '21 at 14:46