How to manage TList i FPC lazarus / 31-LCL ---------------------------------------------------------------------- arneolav · 2013-12-29 23:34 How to manage TList i FPC It seems handling of TList is a little diferent from Delphi: I have a record defined like this:   (This is Delphi code:) Code: [Select]Type   TMainRec = record          Word1,          Word2 : String; .         Counter1, .             : Integer; //a lot of fields ... diferent datatypes end; pMainRec = ^TMainRec; var pwRec: pMainRec; var MainRec : TList; ....... //Create: MainRec := TList.Create; //Fill List: MainRec.Capacity :=  MainRec.Capacity + 1; New(pwRec);  pwRec.Word1    :=  ...stringValue;      //assign values to fields in the record  pwRec.Counter1:=  ...integerValue;      //assign values to fields in the record .....more fields... MainRec.Add(pwRec);  .... While (i <  MainRec.Count) do            //Get values pwRec:= MainRec[i];                          // positioning the list MyWord := pwRec.Word1 ;                 //<---  {$MODE Delphi}  = Ok,  MyInteger := pwRec.Counter1 ;         //<---  {$MODE Delphi}  = Ok .......  for i := 0 to (MainRec.Count -1) do    // Cleanup       begin           pwRec := MainRec.Items[i];           Dispose(pwRec);    {Releases memory allocated for a dynamic variable.}       end;       MainRec.Free This is the Delphi solution, See Lazarus solution below Evrything seems ok, but when i remove {$MODE Delphi} I'v got this FPC error in "MyWord := pwRec.Word1": Illegal qualifier Hint: may be pointer dereference is missing Fatal: Syntax error, ")" expected but "identifier INENDCHAR" found I want to remove all {$MODE Delphi}  but how can I get this to work in a simular way? See Lazarus solution below ---------------------------------------------------------------------- User137 · 2013-12-29 23:57 This works? Code: [Select]MyWord := pwRec^.InWord; Variable that pointer refers to is marked with ^ symbol. Delphi knows that syntax too, it's just being smart about it, assuming that programmer knows he's dealing with a pointer. As for how to add items to a TList, use Add() or Insert() functions. ---------------------------------------------------------------------- Leledumbo · 2013-12-30 03:13 It's not TList that is different, but the pointer dereference. In Delphi (and Turbo Pascal), most of the time you can omit pointer dereference symbol (^) and the compiler will implicitly dereference the pointer. In contrast with FPC and ObjFPC mode, the pointer dereference symbol is compulsory, for consistency I guess. FPC syntax will work everywhere, while Delphi/TP syntax will only work on them or in respective FPC compatibility mode. ---------------------------------------------------------------------- arneolav · 2013-12-30 23:09 This solution seems to work ok: Code: [Select]Type   TMainRec = class      // in Delphi : record          Word1,          Word2 : String; .         Counter1,  ....             : Integer; //a lot of fields ... diferent datatypes end; //pMainRec = ^TMainRec;            //Remove Delphi code //var pwRec: pMainRec;                //Remove Delphi code var MainRec : TList;       pwRec     : TMainRec; ....... //Create: MainRec := TList.Create; //Fill the List: MainRec.Capacity :=  MainRec.Capacity + 1; pwRec := TMainRec.Create;     //New(pwRec);  pwRec.Word1    :=  ...stringValue;      //assign values to fields in the record  pwRec.Counter1:=  ...integerValue;      //assign values to fields in the record .....more fields... MainRec.Add(pwRec);  .... While (i <  MainRec.Count) do               //Get values       pwRec:= TMainRec(MainRec[i]);     // positioning the list,  (Delphi: pwRec:= MainRec[i];)                                      MyWord := pwRec.Word1 ;                MyInteger := pwRec.Counter1 ;      .......                                        {Release memory.    Important: count down to 0}  for i:=MainRec.Count-1 downto 0 do    TMainRec(MainRec[i]).Free;         MainRec.Clear;        MainRec.Free;