[ SOLVED ] populate of specialized fgl list from function lazarus / 21-General ---------------------------------------------------------------------- superc · 2022-02-07 11:22 Hello, for understand fgl I wrote a simple program like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fgl; type   TCharSet = Set of Char;   TElement = class    code      : string;    version   : Integer;    path      : string;  end;   TListElement = specialize TFPGObjectList;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Memo1: TMemo;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private   public    list: TListElement;     function GenerateString(len: SizeInt; const Alphabet: TCharSet): String;    function createElement(strCode: string; ver: integer; strPath: string): TElement;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;  e: TElement;begin  for i := 0 to 100 do  begin     list.add(createElement(GenerateString(10, ['A'..'Z']),                         1,                           GenerateString(10, ['A'..'Z']))    );   end;  end; procedure TForm1.Button2Click(Sender: TObject);var  i: Integer;begin  for i := 0 to list.count -1 do  begin    Memo1.Lines.Add('A:'+list[i].code+'  B:'+list[i].path);  end;  end; procedure TForm1.FormCreate(Sender: TObject);begin  list := TListElement.Create(true);end; function TForm1.GenerateString(len: SizeInt; const Alphabet: TCharSet): String;var  i: SizeInt;  c: Char;begin  SetLength(Result, len);  i := 0;  while i < len do  begin    c := chr(Random(256));    if not (c in Alphabet) then      Continue;    Result[i + 1] := c;    Inc(i);  end;end; function TForm1.createElement(strCode: string; ver: integer; strPath: string): TElement;var  e: TElement;begin  e := TElement.Create;  e.code    := strCode;  e.path    := strPath;  e.version := 1;   Result := e;end; end.  Program works fine, but I've question: for populate specialized list I use a function that return a TElement... It's a correct way in FPC?I remember that in c I had to specify STATIC to remedy the fact that the compiler did not make optimizations.... Thanks in advance. ---------------------------------------------------------------------- PascalDragon · 2022-02-07 12:23 You can just as well add a constructor to your TElement that does the same as your createElement and call that instead: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // in interfacetype  TElement = class    code      : string;    version   : Integer;    path      : string;    constructor Create(strCode: string; ver: integer; strPath: string);  end; // in implementationconstructor TElement.Create(strCode: string; ver: integer; strPath: string);begin  code := strCode;  version := ver;  path := strPath;end; // in your Button1Click    list.add(TElement.Create(GenerateString(10, ['A'..'Z']),                             1,                           GenerateString(10, ['A'..'Z']))    );  What you might want to consider however is if you really want/need to use a class or if a record might be better (then you need to use TFPGList<> or Generics.Collections.TList<> instead of TFPGObjectList<>). ---------------------------------------------------------------------- superc · 2022-02-07 13:25 well, little detail that I had not considered, the constructor returns the element and I can pass it directly to the list ...  Thank you