Mega Code Archive

 
Categories / Delphi / Forms
 

Get Enum information Using Delphis RTTI

Title: Get Enum information Using Delphi's RTTI Tip Submitted by Jens Borrisholt Delphi's RTTI methods expose information about an object's data type that is set into memory at run-time. RTTI provides a way to determine if an object's type is that of a particular class or one of its descendants. In Delphi, enumeration types or Enum provides a way of to define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. Type info on Enums A sample application shows how to "dive" into Delphi enumerated types and values using RTTI to get the values as string to get the number of elements, ...: Download Enum RTTI sample application. unit MainFrm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TCountryLocale = (Croatia, Germany, Denmark, Norway, Sweden, USA) ; TFolioType = (ftNotDefined, ftSales, ftInvoice, ftRoom, ftHall, ftTable) ; TMainForm = class(TForm) lbEnumTypes: TListBox; MemoInfo: TMemo; procedure FormCreate(Sender: TObject) ; procedure lbEnumTypesClick(Sender: TObject) ; end; var MainForm: TMainForm; implementation uses TypInfo, Buttons; {$R *.DFM} function EnumToStrIng(const TypeInfo: pTypeInfo; Ix: Integer): string; begin Result := GetEnumName(TypeInfo, ix) ; end; procedure TMainForm.FormCreate(Sender: TObject) ; begin with lbEnumTypes.Items do begin AddObject('TCountryLocale', TypeInfo(TCountryLocale)) ; AddObject('TFolioType', TypeInfo(TFolioType)) ; AddObject('Boolean', TypeInfo(Boolean)) ; AddObject('TPenStyle', TypeInfo(TPenStyle)) ; end; lbEnumTypes.ItemIndex := 0; lbEnumTypesClick(Self) ; Caption := EnumToStrIng(TypeInfo(TCountryLocale), Integer(Denmark)) ; end; procedure TMainForm.lbEnumTypesClick(Sender: TObject) ; var OrdTypeInfo: PTypeInfo; OrdTypeData: PTypeData; TypeNameStr: string; TypeKindStr: string; MinVal, MaxVal: Integer; ix: Integer; begin MemoInfo.Lines.Clear; with lbEnumTypes do begin {- Get the TTypeInfo pointer } OrdTypeInfo := PTypeInfo(Items.Objects[ItemIndex]) ; { - Get the TTypeData pointer } OrdTypeData := GetTypeData(OrdTypeInfo) ; {- Get the type name string } TypeNameStr := OrdTypeInfo.Name; {- Get the type kind string } TypeKindStr := GetEnumName(TypeInfo(TTypeKind), Integer(OrdTypeInfo^.Kind)) ; {- Get the minimum and maximum values for the type } MinVal := OrdTypeData^.MinValue; MaxVal := OrdTypeData^.MaxValue; {- Add the information to the memo } with MemoInfo.Lines do begin Add('Type Name: ' + TypeNameStr) ; Add('Type Kind: ' + TypeKindStr) ; Add('Min Val: ' + IntToStr(MinVal)) ; Add('Max Val: ' + IntToStr(MaxVal)) ; {- Show the values and names of the enumerated types } if OrdTypeInfo^.Kind = tkEnumeration then for ix := MinVal to MaxVal do Add(Format(' Value: %d Name: %s', [ix, GetEnumName(OrdTypeInfo, ix)])) ; end; end; end; end. The code displays RTTI info using two custom-defined enumerations: TCountryLocale and TFolioType plus two predefined Delphi enums: Boolean and TPenStyle. Add your own enum type, or any other enum types, to the OnCreate event handler (with lbEnumTypes.Items do ...) to see what the "enum is made of"... Also, note that you can convert an enum value to string[link] and also a string representation of an enum back to its ordinal value.