11

Is it possible to cast/convert an Enumeration Value to Integer in Delphi?

If yes, then how?

Shaun Roselt
  • 1,650
  • 5
  • 18
  • 44

4 Answers4

27

This is called out explicitly at the documentation for enumerated types:

Several predefined functions operate on ordinal values and type identifiers. The most important of them are summarized below.

| Function |                       Parameter                       |                      Return value | Remarks                                           |
|----------|:-----------------------------------------------------:|----------------------------------:|---------------------------------------------------|
| Ord      |                   Ordinal expression                  |  Ordinality of expression's value | Does not take Int64 arguments.                    |
| Pred     |                   Ordinal expression                  | Predecessor of expression's value |                                                   |
| Succ     |                   Ordinal expression                  |   Successor of expression's value |                                                   |
| High     | Ordinal type identifier or   variable of ordinal type | Highest value in type             | Also operates on short-string   types and arrays. |
| Low      | Ordinal type identifier or   variable of ordinal type | Lowest value in type              | Also operates on short-string   types and arrays. |
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
17

I see David has posted you a good answer while I was writing this, but I'll post it anyway:

program enums;
{$APPTYPE CONSOLE}
uses
  SysUtils, typinfo;
type
  TMyEnum = (One, Two, Three);
var
  MyEnum : TMyEnum;
begin
  MyEnum := Two;
  writeln(Ord(MyEnum));  // writes 1, because first element in enumeration is numbered zero

  MyEnum := TMyEnum(2);  // Use TMyEnum as if it were a function
  Writeln (GetEnumName(TypeInfo(TMyEnum), Ord(MyEnum)));  //  Use RTTI to return the enum value's name
  readln;
end.
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • 1
    You have done a great job posting this 'anyway' because other experts don't have an inkling how easy it is to confuse what an Enum variable speaks about. There's the doc of course but many reasons to skip it. "One example saves a ton of theory". – user30478 May 10 '20 at 07:13
  • I have already up-voted this long ago, but somehow needed to see it again. Thanks for the reply. – user30478 May 10 '20 at 07:19
  • @MartynA I love your succinct explanation on uses of RTTI. Please consider writing a Delphi blog. – Michael Riley - AKA Gunny May 25 '20 at 11:09
  • @MichaelRiley-AKAGunny: That's very kind. Actually, prompted by an earlier comment of yours and one from Dave Nottage, I'm thinking seriously about it. In fact, the past few days I've been working on a ToolsApi project that might be interesting enough to be worth blogging about. Watch this space, as they say. – MartynA May 25 '20 at 14:45
4

Casting the enum to an integer works. I could not comment on the other answers so posting this as an answer. Casting to an integer may be a bad idea (please comment if it is).

type
  TMyEnum = (zero, one, two);
var
  i: integer;
begin
  i := integer(two); // convert enum item to integer
  showmessage(inttostr(i));  // prints 2
end;

Which may be similar to Ord(), but I am unsure which is the best practice. The above also works if you cast the enum to an integer

type
  TMyEnum = (zero, one, two);
var
  MyEnum: TMyEnum;
  i: integer;
begin
  MyEnum := two; 
  i := integer(MyEnum);      // convert enum to integer
  showmessage(inttostr(i));  // prints 2
end;
Another Prog
  • 841
  • 13
  • 19
  • Compare `ShowMessage(IntToStr(High(Ord(two))))` with `ShowMessage(IntToStr(High(Integer(two))))`. – Sertac Akyuz Jun 27 '17 at 22:49
  • Is the Ord() function a leftover from turbopascal days that people are using when there were no casts available? Would be interesting to know the reasons for when and when not to use a cast vs Ord().... AFAIR at one point the pascal language had no casting ability and special functions were used instead, but I'm not old enough to remember or know for certain :-) – Another Prog Jun 28 '17 at 02:59
  • @AnotherProg, about the best practice between `Ord(MyEnum)` and `Integer(MyEnum)`, I made an explicit question to @David. Pls see the comments of the David's answer. – Delmo Jun 25 '18 at 15:30
0

You can use the Ord() function for this. For clarity it may be better to write a pair of IntToEnum() and EnumToInt() functions though.

Owen
  • 90
  • 1
  • 2
  • 5