Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
Unit StringList create and destroy [SOLVED]
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
We are starting to use La...
by
dbannon
[
Today
at 06:25:04 am]
Fast Canvas Library V1.05...
by
backprop
[
Today
at 03:19:31 am]
TurboBird for FireBird 5
by
maurog
[
Today
at 02:28:10 am]
Status of FPC 3.4.0 or FP...
by
Graeme
[
Today
at 01:53:52 am]
TurboBird IBX
by
maurog
[
Today
at 01:53:39 am]
lazarus structure
by
crownfield
[
Today
at 12:28:23 am]
Delphi Magazine issues 1-...
by
simone
[February 16, 2026, 11:49:11 pm]
LazSerial not found
by
CM630
[February 16, 2026, 10:00:08 pm]
Duplicated icon in the Wi...
by
n7800
[February 16, 2026, 09:31:22 pm]
"PPU file isn't found by ...
by
Martin_fr
[February 16, 2026, 09:29:59 pm]
Reporting a Bug? in Strin...
by
Bart
[February 16, 2026, 06:47:13 pm]
[ANN] fpGUI Toolkit v2.0....
by
cdbc
[February 16, 2026, 06:45:05 pm]
Free Pascal for a small e...
by
devEric69
[February 16, 2026, 06:13:48 pm]
Datatype of Fields in sql...
by
Hansvb
[February 16, 2026, 06:10:23 pm]
Onguard Win64
by
AgriMensor
[February 16, 2026, 05:55:41 pm]
Duplicate identifier erro...
by
Martin_fr
[February 16, 2026, 04:03:10 pm]
Frustrating Error When us...
by
TYDQ
[February 16, 2026, 02:50:02 pm]
[AGGPas] Difference betwe...
by
wp
[February 16, 2026, 02:15:23 pm]
[AGGPas] Usage of scale m...
by
Roland57
[February 16, 2026, 11:33:08 am]
makefiles
by
valdir.marcos
[February 16, 2026, 11:24:17 am]
ThorVG - test (lightweigh...
by
valdir.marcos
[February 16, 2026, 11:19:41 am]
Problems in drawing an ar...
by
valdir.marcos
[February 16, 2026, 11:18:39 am]
AI to port DBDesigner For...
by
Graeme
[February 16, 2026, 10:26:15 am]
Select rectangle of the i...
by
valdir.marcos
[February 16, 2026, 10:01:09 am]
DCPcrypt v2.0.6 — Cryptog...
by
valdir.marcos
[February 16, 2026, 09:55:55 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Unit StringList create and destroy [SOLVED] (Read 1246 times)
Pe3s
Hero Member
Posts: 641
Unit StringList create and destroy [SOLVED]
«
on:
November 27, 2024, 10:58:56 pm »
Hello forum members, how can I create an object in a separate unit module so as not to create and delete it in Form Create and Destroy.
I want it to be created and destroyed in the unit module.
Code: Pascal
[Select]
[+]
[-]
FImages
:
=
TStringList
.
Create
;
«
Last Edit: November 28, 2024, 09:03:57 am by Pe3s
»
Logged
ackarwow
Full Member
Posts: 174
Re: Unit StringList create and destroy
«
Reply #1 on:
November 27, 2024, 11:03:53 pm »
Hi @Pe3s
For example in this way:
Code: Pascal
[Select]
[+]
[-]
unit
TestUnit
;
{$mode ObjFPC}{$H+}
interface
uses
Classes
,
SysUtils
;
var
FImages
:
TStrings
;
implementation
initialization
FImages
:
=
TStringList
.
Create
;
finalization
if
Assigned
(
FImages
)
then
FreeAndNil
(
FImages
)
;
end
.
Logged
Pe3s
Hero Member
Posts: 641
Re: Unit StringList create and destroy
«
Reply #2 on:
November 27, 2024, 11:07:08 pm »
@ackarwow
Hello, I wonder if using constructor and destructor will also be good ?
Logged
cdbc
Hero Member
Posts: 2625
Re: Unit StringList create and destroy
«
Reply #3 on:
November 27, 2024, 11:07:21 pm »
Hi
Another example:
Code: Pascal
[Select]
[+]
[-]
unit
Unit1
;
{$mode ObjFPC}{$H+}
interface
uses
Classes
,
SysUtils
;
function
GetImageList
:
TStringList
;
implementation
var
FImages
:
TStringList
=
nil
;
function
GetImageList
:
TStringList
;
begin
if
not
Assigned
(
FImages
)
then
FImages
:
=
TStringList
.
Create
;;
Result
:
=
FImages
;
end
;
finalization
FImages
.
Free
;
end
.
Regards Benny
Logged
If it ain't broke, don't fix it
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release & FPC Main -> Lazarus Main
ackarwow
Full Member
Posts: 174
Re: Unit StringList create and destroy
«
Reply #4 on:
November 27, 2024, 11:14:23 pm »
Quote from: Pe3s on November 27, 2024, 11:07:08 pm
@ackarwow
Hello, I wonder if using constructor and destructor will also be good ?
Yes, you can use constuctor and destructor directly, although in case of destructor I prefer use it indirectly (via MyStrings.Free or FreeAndNil(MyStrings)).
Logged
Pe3s
Hero Member
Posts: 641
Re: Unit StringList create and destroy
«
Reply #5 on:
November 28, 2024, 09:03:45 am »
Thank you
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
Unit StringList create and destroy [SOLVED]
TinyPortal
© 2005-2018