cebasso
asked on
Storing a record in a TList with different struct
Hello,
What is the best way to identify wich kind of record is in the list?
Example
type
TSomethingA = record //A = ANSI
chBla: array [0..9] of AnsiChar;
end;
PSomethingA = ^TSomethingA;
TSomethingW = record //W = UNICODE
chBla: array [0..9] of WideChar;
end;
PSomethingW = ^TSomethingW;
//global var
var
lsList: TList;
//the lsList is created and inside has a lot of PSomethingW and PSomethingA stored
procedure Something;
var
SomethingA: PSomethingA;
SomethingW: PSomethingW;
i: Integer;
begin
//now, walking in lsList, an item can be of type SomethingA or SomethingW
for i := 0 to Pred(lsList.Count) do
//what is the best method to identify, if the current item "lsList[ i ]" is a SomethingA or SomethingW?
end;
end;
What is the best way to identify wich kind of record is in the list?
Example
type
TSomethingA = record //A = ANSI
chBla: array [0..9] of AnsiChar;
end;
PSomethingA = ^TSomethingA;
TSomethingW = record //W = UNICODE
chBla: array [0..9] of WideChar;
end;
PSomethingW = ^TSomethingW;
//global var
var
lsList: TList;
//the lsList is created and inside has a lot of PSomethingW and PSomethingA stored
procedure Something;
var
SomethingA: PSomethingA;
SomethingW: PSomethingW;
i: Integer;
begin
//now, walking in lsList, an item can be of type SomethingA or SomethingW
for i := 0 to Pred(lsList.Count) do
//what is the best method to identify, if the current item "lsList[ i ]" is a SomethingA or SomethingW?
end;
end;
ASKER
hey epasquier!
cool!
but, the struct has more members like
chBla: array [0.9] of WideChar;
dwFlags: DWORD;
iSomething: Integer;
//and more
no problem?
and actually i'm using New() to store in TList, like
var
SomethingA: PSomethingA;
begin
New(SomethingA);
FillChar(SomethingA^, SizeOf(TSomethingA), 0);
//fill members
lsList.Add(SomethingA);
Dispose(SomethingA);
end;
now, with your code, i need just to change PSomethingA to TSomethingA as a class, do the same and store in a TObjectList instead TList like your example?
cool!
but, the struct has more members like
chBla: array [0.9] of WideChar;
dwFlags: DWORD;
iSomething: Integer;
//and more
no problem?
and actually i'm using New() to store in TList, like
var
SomethingA: PSomethingA;
begin
New(SomethingA);
FillChar(SomethingA^, SizeOf(TSomethingA), 0);
//fill members
lsList.Add(SomethingA);
Dispose(SomethingA);
end;
now, with your code, i need just to change PSomethingA to TSomethingA as a class, do the same and store in a TObjectList instead TList like your example?
no, you can't create objects with New, and do not use FillChar on them.
It's possible that your needs are not compatible with Objects... So you might have to do it the hard way, with a Type field in records.
I wont have time to explain you that alternate today, I'll think of the best thing for you and explain monday
It's possible that your needs are not compatible with Objects... So you might have to do it the hard way, with a Type field in records.
I wont have time to explain you that alternate today, I'll think of the best thing for you and explain monday
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How about using records with variant parts example:
TPerson = record
FirstName : string[40];
LastName : string[40];
{ Fixed portion of record begins here }
BirthDate: TDate;
case
Citizen: Boolean of
{ variant portion of record begins here }
True: (BirthPlace: string[40]);
False:
(
Country: string[20];
EntryPort: string[20];
EntryDate: TDate;
ExitDate: TDate
);
end;
TPerson = record
FirstName : string[40];
LastName : string[40];
{ Fixed portion of record begins here }
BirthDate: TDate;
case
Citizen: Boolean of
{ variant portion of record begins here }
True: (BirthPlace: string[40]);
False:
(
Country: string[20];
EntryPort: string[20];
EntryDate: TDate;
ExitDate: TDate
);
end;
ASKER
Thanks!
Easiest way : use class instead
Open in new window