Fandom

Delphi Programming

GetEnumName Routine

2,918pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

This page is intended as a supplement to the official documentation on Delphi programming. CodeGear is in the process of putting the Delphi documentation on the Web. Once they have done so, this page will link to the relevant page in the official documentation.
Stub
This article is a stub.
Please help enhance the Delphi Programming Wiki by expanding it.
Info
see the VCL Documentation Guidelines for an overview on doc pages

UnitEdit

DescriptionEdit

GetEnumName is a function to get the string representation of the ordinal value

The first parameter (TypeInfo: PTypeInfo) is a pointer to the typeinfo of the enumeration type.

The second parameter (Value: Integer) is the ordinal value of the enumeration type.

The result is the enumeration value name in string form.

Technical CommentsEdit

Be aware that type information will not be available when the enumeration is given non-contiguous or out-of-order ordinal values. The lowest value in the enumeration must also be zero; i.e. ordinal values can only match the values the enumeration would have by default.

With the enumeration below it's not possible to use GetEnumValue, and TypeInfo(TTestType) will result in 'E2134 Type 'TTestType' has no type info':

type
  TTestType = (ttTest1 = 2, ttTest2 = 4, ttTestUnknown = 6);

However, the following will work as expected:

type
  TTestType = (ttTest1 = 0, ttTest2 = 1, ttTestUnknown = 2);

ExamplesEdit

Name by integer constant

type
  TTestType = (ttTest1, ttTest2, ttTestUnknown);
...
  ShowMessage(GetEnumName(TypeInfo(TTestType), 2)); //will show 'ttTestUnknown'

Name through variable values, you need to typecast to an integer.

type
  TTestType = (ttTest1, ttTest2, ttTestUnknown);
var
  MyTestType: TTestType;
...
  MyTestType:= ttTestUnknown;
  ShowMessage(GetEnumName(TypeInfo(TTestType), Integer(MyTestType))); //will show 'ttTestUnknown'

See AlsoEdit

User Comments/TipsEdit

(Please leave your name with your comment.)

Also on Fandom

Random Wiki