It is possible to declare enums with custom values in Delphi 5 like this?:
type
MyEnum = (meVal1 = 1, meVal2 = 3); // compiler error
Thanks!
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'.
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.
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);
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.
In older Delphis you can do
type
MyEnum = (meUnused1, meVal1, meUnused2, meVal2);
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With