Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-09

TFPObjectlist example

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

Am I correct that TFPObjectlist is kind of like Tstringlist, except instead of creating and array of strings, it creates an array of objects?

I'm wanting to do something like that but I don't know how to define the object.. I'm guessing it would be something like a record

I'm wanting to end up with a list of records like:
Type
   test = record
      Port             :String;
      Size             :Byte;
      Status           :Word;
   End;

Does anyone have an example of how to use TFPObjectlist?

James

Reply by email

Ryan Joseph
to James Richters
On Sep 7, 2020, at 6:07 AM, James Richters via fpc-pascal wrote: Does anyone have an example of how to use TFPObjectlist?

It just frees objects that are removed from the list (or when the list is freed).

list:= TFPObjectlist.Create;
list.Add(TObject.Create);
list.Free;

in that example the object added to the list won't leak memory but it will be freed when the list itself is freed.

or

list:= TFPObjectlist.Create;
list.Add(TObject.Create);
list.Delete(0);

will also not leak memory.

Regards,
Ryan Joseph

Reply by email

Vojtěch Čihák
to James Richters: Am I correct that TFPObjectlist is kind of like Tstringlist, except instead of creating…

Hi,
 
I used TFPObjectList only once, for visual component TECAccordion (https://wiki.lazarus.freepascal.org/Eye-Candy_Controls#TECAccordion). Each item of list is derived from TWinControl so it is possible to use it even for visual components (like TCollection).  
However, in your case (records) I would rather use TFPGList from FGL unit:  
1) add {$modeswitch ADVANCEDRECORDS}
2) add FGL to uses3) define class operator =test = record    Port :String;    Size :Byte;    Status :Word;    class operator = (A,B: test): Boolean;  End; 4) implement itclass operator test.=(A, B: test): Boolean;begin  Result:=(A.Port=B.Port) and (A.Size=B.Size);  // and ...end;   5) define specialized list:   TMyL = specialize TFPGList<test>;
6) create it
  MyList: TMyL;
  ...
  MyList:=TMyL.Create;
 
Now you can work with it comfortably
MyList[1].Port:='abc';
 
V.
______________________________________________________________

Show quoted text 5 lines
Od: "James Richters via fpc-pascal" Komu: "'FPC-Pascal users discussions'" Datum: 07.09.2020 01:08 Předmět: [fpc-pascal] TFPObjectlist example

James

Reply by email

Vojtěch Čihák
to Vojtěch Čihák

Hi,
 
some crippled formatting, so again:
 
However, in your case (records) I would rather use TFPGList from FGL unit:  
1) add {$modeswitch ADVANCEDRECORDS}
 
2) add FGL to uses
3) define class operator =
test = record    Port :String;    Size :Byte;    Status :Word;    class operator = (A,B: test): Boolean;  End; 4) implement itclass operator test.=(A, B: test): Boolean;begin  Result:=(A.Port=B.Port) and (A.Size=B.Size);  // and ...end;   5) define specialized list:  
  TMyL = specialize TFPGList<test>;
 
6) create it
 
  MyList: TMyL;
 
  ...
 
  MyList:=TMyL.Create;
 
Now you can work with it comfortably
 
MyList[1].Port:='abc';
 
V.
______________________________________________________________

Show quoted text 5 lines
Od: "Vojtěch Čihák via fpc-pascal" Komu: , "FPC-Pascal users discussions" Datum: 08.09.2020 16:21 Předmět: Re: [fpc-pascal] TFPObjectlist example

______________________________________________________________

Show quoted text 5 lines
Od: "James Richters via fpc-pascal" Komu: "'FPC-Pascal users discussions'" Datum: 07.09.2020 01:08 Předmět: [fpc-pascal] TFPObjectlist example

James

Reply by email

James Richters
to Vojtěch Čihák

What’s the difference between TList and TFPGList? Which one is better for this example?

James

Earlier message 61 lines
From: fpc-pascal On Behalf Of Vojtech Cihák via fpc-pascal Sent: Tuesday, September 8, 2020 10:29 AM To: FPC-Pascal users discussions Cc: Vojtěch Čihák Subject: Re: [fpc-pascal] TFPObjectlist example Hi, some crippled formatting, so again: However, in your case (records) I would rather use TFPGList from FGL unit: 1) add {$modeswitch ADVANCEDRECORDS} 2) add FGL to uses 3) define class operator = test = record Port :String; Size :Byte; Status :Word; class operator = (A,B: test): Boolean; End; 4) implement it class operator test.=(A, B: test): Boolean; begin Result:=(A.Port=B.Port) and (A.Size=B.Size); // and ... end; 5) define specialized list: TMyL = specialize TFPGList<test>; 6) create it MyList: TMyL; ... MyList:=TMyL.Create; Now you can work with it comfortably MyList[1].Port:='abc'; V. ______________________________________________________________ > Od: "Vojtěch Čihák via fpc-pascal" < > > Komu: , "FPC-Pascal users discussions" < > > Datum: 08.09.2020 16:21 > Předmět: Re: [fpc-pascal] TFPObjectlist example > ______________________________________________________________ > Od: "James Richters via fpc-pascal" < > > Komu: "'FPC-Pascal users discussions'" < > > Datum: 07.09.2020 01:08 > Předmět: [fpc-pascal] TFPObjectlist example > James

Reply by email

Sven Barth
to James Richters

James Richters via fpc-pascal schrieb am
Di., 8. Sep. 2020, 20:07:

What’s the difference between TList and TFPGList? Which one is better for this example?

The main difference is that TFPGList ist a generic, thus when you specialize it with your record you'll have a typesafe object where you don't need to manually work with pointers while for TList you either need to work with pointers or declare a descendant with typesafe setters/getters like you had done in the other mail.
You could also take a look at the generic TList in Generics.Collections which does not require an = operator like TFPGList does.

Please note that if you work with TFPGList or the Generics.Collection.TList then you need to modify an entry using a local variable if your entries are records:

entry := mylist[i];
entry.field := entry.field * 2;
mylist[i] := entry;

This is because the getter for the Items property will provide a /copy/ of the entry.

With Classes.TList you'd use a pointer however and thus you can modify it in place.

Regards,
Sven

Reply by email