tags:

views:

186

answers:

1

This single line of code:

ShowMessage(GetEnumName(TypeInfo(TAlign), 1));

returns "alTop".

How can I get all values of enumerated type into stringlist, when I want to use string variable: 'TAlign' instead of TAlign? Something like:

ShowMessage(GetEnumName(TypeInfo('TAlign'), 1));

Thanx

+3  A: 

To be able to use a string variable, you'd need to register the TypeInfo with the string in some sort of lookup table, and then look it up.

To get all the enumerated type names in your list, you can do something like this:

procedure LoadAllEnumValuesIntoStringList(enum: PTypeInfo; list: TStringList);
var
   data: PTypeData;
   i: integer;
begin
   list.clear;
   data := GetTypeData(GetTypeData(enum)^.BaseType^);
   for i := 0 to data.MaxValue do
      list.add(GetEnumName(enum, i));
end;
Mason Wheeler
Maybe it is possible to walk a chain of typeinfo's? Are they linked or tabled maybe? Anyway +1 for the only compiler and -version agnostic solution
Marco van de Voort
it is not exactly what I wanted. I do not know how to change string to PTypeInfo. May you try to rewrite it ni the way like:procedure LoadAllEnumValuesIntoStringList(Enum:String; list: TStringList);I need this function in order to make similar object inspector like in delphi. I want to use it in run time. I can get all published properties and their types of an object as stringlist. That§s why type TAlign is written as 'TAlign'. I want to take this string ('TAlign'), pass it to appropriate procedure, which fills combobox with all members of TAlign. So I can edit items easily.
lyborko
There is a mistake. You should write:data := GetTypeData(GetTypeData(enum)^.BaseType^);You are very good to write it by hand without compiler...
lyborko
Oh, well if you're looking for an Object Inspector, I believe the JVCL has one. Try looking through the code and seeing how it works.
Mason Wheeler