Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return all enum items as a stringlist

My function EnumToStringList on its own can be compiled, but I can’t call it with any enum type, in my app.

Error : **[dcc64 Error] Unit_test.pas(119): E2029 '(' expected but ')' found**

What is wrong with my approach ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function EnumToStringList(const TypeInfo: pTypeInfo): TStringlist;
    var  i : Integer;
    
    begin
      Result := TStringList.Create;
      for i := GetTypeData(TypeInfo)^.MinValue to GetTypeData(TypeInfo)^.MaxValue do
           begin
             result.add (GetEnumName(TypeInfo, i));
           end;
    
    end;
    
    
    function EnumToString(const TypeInfo: pTypeInfo; Ix: Integer): string;
    begin
      result := GetEnumName(TypeInfo, Ix);
    end;


   ///  test code : 
   TProjectTypes = (low,hot,skip,pause,others);

   MyProjectStrings := EnumToStringList (TProjectTypes);

>Solution :

You cannot directly pass enumeration type to your function. You need to get its type info first by calling TypeInfo on it.

  var
    MyProjectStrings := EnumToStringList(TypeInfo(TProjectTypes));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading