[fpc-pascal] Enum RTTI

Wolfram Kläger wolfram.klaeger at web.de
Thu Nov 5 11:10:24 CET 2009


> -----Ursprüngliche Nachricht-----
> Von: "Wimpie Nortje" <wimpienortje at gmail.com>
> Gesendet: 05.11.09 09:33:36
> An: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
> Betreff: [fpc-pascal] Enum RTTI


> I very often have enums for which I want string descriptions. I couldn't 
> find a way to use RTTI to obtain the string descriptions so I resorted 
> to having an array of strings. The problem is that the enums change 
> frequently in item count and item order, which means I spend a lot of 
> time fixing the string<->enum functions .

Wimpie,

maybe you want to try:

uses
  typinfo;

// implementation

function Str(const AEnum : TEnum) : String; overload;
begin
  Result := GetEnumName(TypeInfo(TEnum), ord(AEnum));
end;

I usually declare one such overload per enum type. If you are using sysutils.Str, you have to avoid conflicts by chosing a different function name, e.g. EnumStr.

Wolfram



More information about the fpc-pascal mailing list