skip to content
Nick Hodges Nick Hodges

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.

Powered by Respectify

Comments are analyzed in real time for spam and respectfulness before they appear. Leave a comment to see Respectify in action.

Try the demo →

Comments ()

Loading comments…

No comments yet. Be the first to comment!

Leave a Comment

Your comment will be reviewed by Respectify before it appears.