[fpc-pascal] How can you convert an enum value to string?

Sven Barth pascaldragon at googlemail.com
Mon Aug 30 13:39:09 CEST 2010


Am 30.08.2010 12:37, schrieb Graeme Geldenhuys:
> Op 2010-08-30 12:30, Frank Church het geskryf:
>> Is there a subroutine that can convert an enum value to a strings, so
>> that I can do something like ShowMessage(EnumToStr(enumValue))?
>
>
> eg:
>
> function ObjectStateToString(AObjectState : TPerObjectState): string;
> begin
>    result := GetEnumName(TypeInfo(TPerObjectState),
>                           Ord(AObjectState));
> end;
>
>
> GetEnumName is defined in 'typinfo' unit.

Another possibility is this (requires 2.4.0 or newer):

function ObjectStateToString(AObjectState : TPerObjectState): string;
begin
   WriteStr(Result, AObjectState);
end;

WriteStr works the same as Write, but with a String as a argument 
instead of a File. See here: 
http://www.freepascal.org/docs-html/rtl/system/writestr.html

Regards,
Sven



More information about the fpc-pascal mailing list