Jump to content
chkaufmann

Generic record helper for enumeration types

Recommended Posts

For many enumeration type I have I define the same set of methods. Here is an example:

  TBSSwGender = (sgeNone, sgeMen, sgeWomen, sgeMixed);

  TBSSwGenderHelper = record helper for TBSSwGender
  public
    function AsInteger: Integer;
    function Code: String;
    function Name: String;
  end;

function TBSSwGenderHelper.AsInteger: Integer;
begin
  Result := Ord(Self);
end;

function TBSSwGenderHelper.Code: String;
begin
  Result := '#Enumeration.BSSwGenderCode%d'.Fmt([AsInteger]).Translate;
end;

function TBSSwGenderHelper.Name: String;
begin
  Result := '#Enumeration.BSSwGenderName%d'.Fmt([AsInteger]).Translate;
end;

 

Now my question is, instead of writing the same code for all types. So something like this:

TBSEnumHelper<T> = record helper for T
  function AsInteger: Integer;
  ...
end;

TBSEnumHelper<TBSSwGender> = record helper for TBSSwGender

Christian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×