Recent

Author Topic: TSringList Question  (Read 643 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1169
TSringList Question
« on: July 11, 2022, 05:01:48 am »
Can you Declare a Global TStringList simular to a Global Dynamic array.

I tried to do it the same way but an Access error when I tried to to do TList.Clear;

Code: Pascal  [Select][+][-]
  1. Const
  2.   BaseLeft = 25;    
  3.  
  4. Var
  5.  TList: TStringList;
  6.  
  7. procedure TForm1.FormCreate(Sender: TObject);
  8.  begin
  9.   TList := TStringList.Create;
  10. end;  
  11.  
  12. procedure TForm1.DoSomething;
  13. begin
  14.  TList.Clear;                                 // Access Violation
  15. end;                            
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dseligo

  • Hero Member
  • *****
  • Posts: 715
Re: TSringList Question
« Reply #1 on: July 11, 2022, 07:31:41 am »
You can.
Try to change name, like below. TList is a class defined in 'classes'.

Code: Pascal  [Select][+][-]
  1. Var
  2.   List: TStringList;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. begin
  6.   List := TStringList.Create;
  7. end;  
  8.  
  9. procedure TForm1.DoSomething;
  10. begin
  11.   List.Clear;
  12. end;
« Last Edit: July 11, 2022, 07:33:16 am by dseligo »

PascalDragon

  • Hero Member
  • *****
  • Posts: 4561
  • Compiler Developer
Re: TSringList Question
« Reply #2 on: July 11, 2022, 01:27:18 pm »
Can you Declare a Global TStringList simular to a Global Dynamic array.

I tried to do it the same way but an Access error when I tried to to do TList.Clear;

You should instantiate a global variable in the initialization-section of the unit (and free it in the finalization-section) as otherwise it might be instantiated multiple times if TForm1 is created multiple times. Also this way you can be sure that it's indeed initialized once TForm1.DoSomething runs.

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: TSringList Question
« Reply #3 on: July 11, 2022, 08:12:11 pm »
@PascalDragon Some of us are just Geico's.

It is nice to know it can be do but the answer is above my pay grade.

Never heard of the initialization-section or finalization-section.



FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 4947
Re: TSringList Question
« Reply #4 on: July 11, 2022, 09:43:56 pm »
Don't forget to do List.Free in FormDestroy if you create it in FormCreate.
Otherwise you get a memory leak.

And this code is indeed only safe when there is only one TForm1 created (because a second TForm1 would overwrite the first List instance). To fix that you could also move the List var definition to within the TForm1 class.

winni

  • Hero Member
  • *****
  • Posts: 3134
Re: TSringList Question
« Reply #5 on: July 12, 2022, 12:07:21 am »
@PascalDragon Some of us are just Geico's.

It is nice to know it can be do but the answer is above my pay grade.

Never heard of the initialization-section or finalization-section.


Hi!

Code: Pascal  [Select][+][-]
  1. unit  Whatever;
  2. ...
  3. Var  List: TStringList;
  4. ...
  5.  
  6. Procedure ClearList;
  7. begin
  8. List.clear;
  9. end;
  10.  
  11. ...
  12.  
  13. initialization
  14. List := TStringList.create;
  15.  
  16. finalization
  17. List.free;
  18. end.
  19.  

Read Marco Cantu for further infos.

https://www.marcocantu.com/epascal/English/ch11unit.htm

Winni

JLWest

  • Hero Member
  • *****
  • Posts: 1169
Re: TSringList Question
« Reply #6 on: July 12, 2022, 05:12:20 am »
Hi Wini

Can I assume that the initialization and  finalization can go anywhere after the implementation 

Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

rvk

  • Hero Member
  • *****
  • Posts: 4947
Re: TSringList Question
« Reply #7 on: July 12, 2022, 08:09:28 am »
Can I assume that the initialization and  finalization can go anywhere after the implementation 
No, they need to be directly above the "end." of the unit, so completely at the end.


PascalDragon

  • Hero Member
  • *****
  • Posts: 4561
  • Compiler Developer
Re: TSringList Question
« Reply #8 on: July 12, 2022, 08:51:35 am »
@PascalDragon Some of us are just Geico's.

It is nice to know it can be do but the answer is above my pay grade.

Never heard of the initialization-section or finalization-section.

It might pay of to take a look at the documentation then. ;D

 

TinyPortal © 2005-2018