Reputation: 905
How do I get the string representation if I know the enum of integer value ?
type
MyEnum = (tmp_one, tmp_two, tmp_three);
const
MyTypeNames: array[tmp_one..tmp_three] of string = ('One', 'Two', 'Three');
Upvotes: 0
Views: 238
Reputation: 19356
You don't need to use the ordinal values of your enum type. You can declare an array with the enum type as its "subscript" and use an enum var directly.
type
TMyEnum = (tmp_one, tmp_two, tmp_three);
const
MyTypeNames: array[TMyEnum] of string = ('One', 'Two', 'Three');
function Enum2Text(aEnum: TMyEnum): string;
begin
Result := MyTypeNames[aEnum];
end;
Call it with the enum or an integer value cast to the enum:
Enum2Text(tmp_one); // -> 'One'
Enum2Text(TMyEnum(2)); // -> 'Three'
Upvotes: 4
Reputation: 109003
I assume you want to use the names in your array of string. Then this is very simple:
var
myEnumVar: MyEnum;
begin
myEnumVar := tmp_two; // For example
ShowMessage(MyTypeNames[myEnumVar]);
Upvotes: 6
Reputation: 613461
I'm assuming that you have an ordinal value rather than a variable of this enumerated type. If so then you just need to cast the ordinal to the enumerated type. Like this:
function GetNameFromOrdinal(Ordinal: Integer): string;
begin
Result := MyTypeNames[MyEnum(Ordinal)];
end;
Upvotes: 6