14

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

If yes, then how?

2
  • 3
    I assume you mean enumeration value. Anyway just use Ord(myEnumValue). Commented Mar 29, 2017 at 10:47
  • Please stop editing your own and other users' posts replacing Delphi with "Delphi Programming Language" It is not an improvement of any kind. You can write it in your new posts if you really think this is necessary, but others may edit it out. Commented Apr 26, 2024 at 6:56

4 Answers 4

30

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.
Sign up to request clarification or add additional context in comments.

5 Comments

Then... which could be the best practice Ord(SomeEnumVariable) or Integer(SomeEnumVariable) ?
@Delmo Use Ord and so avoid a cast.
Thanks @David but what is the cons of using a typecast?
@Delmo As a rule, one should avoid typecasting because it breaks type safety. In this case it is safe enough. Why do you not like using Ord?
I like what the best practice claim ;) I just wanna know if there was a preferred way over other. Thanks again!
19

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. 

4 Comments

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".
I have already up-voted this long ago, but somehow needed to see it again. Thanks for the reply.
@MartynA I love your succinct explanation on uses of RTTI. Please consider writing a Delphi blog.
@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.
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; 

3 Comments

Compare ShowMessage(IntToStr(High(Ord(two)))) with ShowMessage(IntToStr(High(Integer(two)))).
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 :-)
@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.
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.