Re: [fpc-pascal] TFPObjectlist example

2020-09-08 Thread Sven Barth via fpc-pascal
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

>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TFPObjectlist example

2020-09-08 Thread James Richters via fpc-pascal
What’s the difference between TList and TFPGList?  Which one is better for this 
example?
 
James
 
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;
 
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"  <mailto:fpc-pascal@lists.freepascal.org> >
> Komu: ja...@productionautomation.net <mailto:ja...@productionautomation.net> 
> , "FPC-Pascal users discussions"  <mailto:fpc-pascal@lists.freepascal.org> >
> Datum: 08.09.2020 16:21
> Předmět: Re: [fpc-pascal] TFPObjectlist example
>
__
> Od: "James Richters via fpc-pascal"  <mailto:fpc-pascal@lists.freepascal.org> >
> Komu: "'FPC-Pascal users discussions'"  <mailto:fpc-pascal@lists.freepascal.org> >
> Datum: 07.09.2020 01:08
> Předmět: [fpc-pascal] TFPObjectlist example
>

James

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org 
<mailto:fpc-pascal@lists.freepascal.org> 
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


--

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org 
<mailto:fpc-pascal@lists.freepascal.org> 
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TFPObjectlist example

2020-09-08 Thread Vojtěch Čihák via fpc-pascal

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;
 
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: ja...@productionautomation.net, "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

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal 
<https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>


--

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal 
<https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TFPObjectlist example

2020-09-08 Thread Vojtěch Čihák via fpc-pascal

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;
6) create it
  MyList: TMyL;
  ...
  MyList:=TMyL.Create;
 
Now you can work with it comfortably
MyList[1].Port:='abc';
 
V.
__

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

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal 


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TFPObjectlist example

2020-09-06 Thread Ryan Joseph via fpc-pascal


> 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

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal