views:

123

answers:

4

Hi! It is possible to declare enums with custom values in Delphi 5 like this?:

type
  MyEnum = (meVal1 = 1, meVal2 = 3); // compiler error

Thanks!

+2  A: 

This is legal according to this article. I do recall that in early versions of Delphi supplying values wasn't supported.

It might help to provide the 'compiler error' you received. Also, what version of Delphi are you using?

Ben Laan
This should indeed be legal, see also: http://docwiki.embarcadero.com/RADStudio/2010/en/Simple_Types#Enumerated_Types_with_Explicitly_Assigned_Ordinality
Otherside
thanks, i'm using D5. Now i see what it is not support this feature... wah...
SomeOne
+2  A: 

If you have an older version of Delphi (<= D5 IIRC) you can't do this. Maybe you can replace the enum by constants? Something like

const
  meVal1 = 1;
  meVal2 = 3;

type
  TMyEnum = Byte; // or Integer or ... - depends on your needs.

Unfortunately, the compiler can't do as much error checking for you with this as with an enum type.

Ulrich Gerhardt
+2  A: 

As a somewhat ugly extension to the answer by Ulrich you could do something like the following:

type
  TMyEnum = (meVal1, meVal2);

const
  MY_ENUM_VALS: array[TMyENum] of integer = (1, 3);

and access them as

if (aVal = MY_ENUM_VALS[meVal2]) then...

Not pretty, I grant you, but at least that way you get a little more compiler error checking for those earlier versions of Delphi.

shunty
+2  A: 

In older Delphis you can do

type
  MyEnum = (meUnused1, meVal1, meUnused2, meVal2);
gabr