Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerations in Delphi with custom values

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

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

Thanks!

like image 481
SomeOne Avatar asked Sep 06 '10 07:09

SomeOne


People also ask

What are enumerations explain with example?

An enumeration is used in any programming language to define a constant set of values. For example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the keyword 'enum'.

What type of data do enumerations store?

An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.

How do you use an enum in Delphi?

An enumerated type defines an ordered set of values. The values themselves have no inherent meaning, they act only as "labels". To declare an enumerated type, use the syntax: type TypeName = (Val1, Val2, ..., Valn);

Can enums contain numbers?

Numeric EnumNumeric enums are number-based enums i.e. they store string values as numbers. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.


3 Answers

In older Delphis you can do

type
  MyEnum = (meUnused1, meVal1, meUnused2, meVal2);
like image 193
gabr Avatar answered Sep 28 '22 07:09

gabr


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?

like image 25
Ben Laan Avatar answered Sep 28 '22 07:09

Ben Laan


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.

like image 41
Uli Gerhardt Avatar answered Sep 28 '22 08:09

Uli Gerhardt