Get enum string values


I have several enums defined in my model, here's one example:.

[Enumeration(TEnumMappingType.emString, 'LTE_4G,LTE_5G,SAT_IRIDIUM')]
TEnumTechnology = (LTE_4G, LTE_5G, SAT_IRIDIUM);

My question is just if there is a way to get the string value from a construct like the above?

In another post here on the support forum, I saw EnumName being mentioned, but I assume there might a unit that needs to be included, but cannot find if there is one. Thus unable to use it.


The unit is TypInfo:

I totally forgot about that one, sorry. Mistakenly I was looking for a TMS/Aurelius related unit.

That being said then GetEnumName in TypInfo will not get the TEnumMappingType string as far as I know.

Here's an enum that better demonstrates my question:
Having this Enum:

  [Enumeration(TEnumMappingType.emString, '1A,2B,3C')]
  TEnumTest = (oneA, twoB, threeC);

With the above enum and using GetEnumName:

  var lEnum := TEnumTest.twoB;
  ShowMessage(GetEnumName(TypeInfo(TEnumTest), integer(LEnum)));

ShowMessage will display 'twoB'

So I guess my question is if there is a method that can get me the '2B' string instead of 'twoB' ?

I guess you could use a const array, but that would be duplicating the code.

BTW, the System.Rtti contains the helper


Hi Russel, yes I could use a const array, but as you mentioned it will duplicate the code.

Usually I add helpers like this:

 TEnumTestHelper = record helper for TEnumTest
    function ToString: string;

{ TEnumTestHelper }
function TEnumTestHelper.ToString: string;
  case Self of
    TEnumTest.oneA:   result := '1A';
    TEnumTest.twoB:   result := '2B';
    TEnumTest.threeC: result := '3C';

But in this case this also duplicates code. So I was hoping to find another solution since I already need to have this in my code:

[Enumeration(TEnumMappingType.emString, '1A,2B,3C')]

But, it's not a showstopper if there is no easy way to do what I want. I just wanted to use this technique in an Integration software that have running that does not use Aurelius.

You could use this:

  Value := Manager.Explorer.MemberScalarValueToDb(EnumType, EnumValue);

Where EnumType type is the TRttiType of the enumerated type, and EnumValue is the TValue containing the enumerated value.

Thank you Wagner!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.