Solved

Storing a record in a TList with different struct

Posted on 2010-09-18
6
687 Views
Last Modified: 2012-05-10
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;
0
Comment
Question by:cebasso
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 33707814
you can't with records, or it would require a complex set of records declarations that would follow a simple rule : having a Type field before (integer)

Easiest way : use class instead
type

  TSomethingA = Class //A = ANSI

    chBla: array [0..9] of AnsiChar;

  end;

//  PSomethingA = ^TSomethingA;



  TSomethingW = Class //W = UNICODE

    chBla: array [0..9] of WideChar;

  end;

//  PSomethingW = ^TSomethingW;



//global var

var

  lsList: TObjectList; // Change TList to TObjectList



//the lsList is created and inside has a lot of PSomethingW and PSomethingA stored

procedure Something;

var

  SomethingA: TSomethingA;

  SomethingW: TSomethingW;

  i: Integer;

begin

  //now, walking in lsList, an item can be of type SomethingA or SomethingW

  for i := 0 to Pred(lsList.Count) do

   if lsList[ i ] is TSomethingA Then // Use Is operator on TObject

    begin

...

    end;

end;

Open in new window

0
 
LVL 4

Author Comment

by:cebasso
ID: 33707836
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?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33707845
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33709026
Alternate method with a field identifying the record. It could be an integer with some constants defined, but that is as well done with an enumerated type.

With that method, you won't have much to change from your older code. I recommend that you use these kind of techniques for API purpose only where you can't use Delphi objects. You can always create objects wrapper around these structures if you use a lot of those in your application.


type

  RecordType=(rtSomethingA,rtSomethingW);

  pRecordType=^RecordType;



  TSomethingA = record //A = ANSI

    RecType:RecordType; // = rtSomethingA

    chBla: array [0..9] of AnsiChar;

  end;

  PSomethingA = ^TSomethingA;



  TSomethingW = record //W = UNICODE

    RecType:RecordType; // = rtSomethingW

    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

  i: Integer;

begin

 for i := 0 to Pred(lsList.Count) do

  Case pRecordType(lsList[i])^ of

   rtSomethingA: With PSomethingA(lsList[i])^ do ... 

   rtSomethingW: With PSomethingW(lsList[i])^ do ... 

  end;

end;

Open in new window

0
 
LVL 8

Expert Comment

by:GeneralTackett
ID: 33712662
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;
0
 
LVL 4

Author Closing Comment

by:cebasso
ID: 33746792
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 103
Delphi XE10, MySQL Query 4 125
DBGrid or StringGrid ? 6 63
Installshield for Embarcadero EX 10.1 Berlin 4 14
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now