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');
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;
MyEnum parameter and avoid the typecast? The types are hard-coded already.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'
T, likeTMyEnum. Also, the members of the enum should be likemeOne, meTwo, meThree. [Compare:TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut); TWindowState = (wsNormal, wsMinimized, wsMaximized)etc.]