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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With