Enumerations in Delphi with Custom Values

You can declare enumerations with custom values ​​in Delphi 5 as follows:

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

Thank!

+3
source share
4 answers

In old Delphis you can do

type
  MyEnum = (meUnused1, meVal1, meUnused2, meVal2);
+4
source

It is legal under this article . Let me remind you that in earlier versions of Delphi delivery values ​​were not supported.

This can help ensure you get a "compiler error." Also, which version of Delphi are you using?

+2
source

​​ Delphi (< = D5 IIRC), . , enum ? -

const
  meVal1 = 1;
  meVal2 = 3;

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

, , .

+2

, - :

type
  TMyEnum = (meVal1, meVal2);

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

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

Not really, I give you, but at least you get a bit more compiler errors for those early versions of Delphi.

+2
source

Source: https://habr.com/ru/post/1763401/


All Articles