Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the type information of a record by its pointer in Delphi XE?

If I have a TList with many pointers of different record types in it, how do I access the values of the differents records within the TList?

Is there any way to get the record type or type information of those referenced recods?

I'm currently using Delphi XE.

like image 515
Haruki Avatar asked Dec 06 '25 10:12

Haruki


1 Answers

Your different record types need a common header. You can then cast the list item pointers to that header type first to determine what record type to cast to next. For example:

type
  TRecType = (recA, recB, recC);

  PRecHeader = ^TRecHeader;
  TRecHeader = record
    RecType: TRecType;
    ...
  end;

  PRecordA = ^TRecordA;
  TRecordA = record
    Header: TRecHeader;
    IntValue: Integer;
  end;

  PRecordB = ^TRecordB;
  TRecordB = record
    Header: TRecHeader;
    StrValue: String;
  end;

  PRecordC = ^TRecordC;
  TRecordC = record
    Header: TRecHeader;
    DblValue: Double;
  end;

var
  PRec: Pointer;
begin
  PRec := List[Index];
  case PRecHeader(PRec)^.RecType of
    recA: use PRecordA(PRec)^.IntValue as needed ...
    recB: use PRecordB(PRec)^.StrValue as needed ...
    recC: use PRecordC(PRec)^.DblValue as needed ...
  end;
end;
like image 97
Remy Lebeau Avatar answered Dec 08 '25 02:12

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!