skip to content

Fun Code of the Day

/ 1 min read

unit uEnumConverter;
interface
type
  TEnum = record
  public
    class function AsString(aEnum: T): string; static;
    class function AsInteger(aEnum: T): Integer; static;
  end;
implementation
uses
      TypInfo
    ;
{ TEnum }
class function TEnum.AsString(aEnum: T): string;
begin
  Result := GetEnumName(TypeInfo(T), AsInteger(aEnum));
end;
class function TEnum.AsInteger(aEnum: T): Integer;
begin
  case Sizeof(T) of
    1: Result := pByte(@aEnum)^;
    2: Result := pWord(@aEnum)^;
    4: Result := pCardinal(@aEnum)^;
  end;
end;
end.

Subscribe to my newsletter

Get things that occur to me delivered straight to your inbox. Totally free, no spam.