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
Some testing the water qu...
by
mdlueck
[
Today
at 03:19:39 pm]
How to hide IEEE754 limit...
by
Thaddy
[
Today
at 03:07:12 pm]
Demo #3 of my "home grown...
by
TBMan
[
Today
at 03:05:57 pm]
Add new unit to a Lazarus...
by
Lulu
[
Today
at 02:58:02 pm]
New Mac mini M4 - FPC 3.2...
by
anse
[
Today
at 02:55:18 pm]
Seems like good timing to...
by
mdlueck
[
Today
at 02:17:23 pm]
Lazarus Bugfix Release 4...
by
jwdietrich
[
Today
at 01:54:34 pm]
synedit
by
Martin_fr
[
Today
at 01:49:12 pm]
[SOLVED] How to update 'F...
by
Hartmut
[
Today
at 01:33:36 pm]
Porting PDF Viewer from D...
by
Boleeman
[
Today
at 12:38:46 pm]
Form scaling aspect ratio
by
Pe3s
[
Today
at 10:32:26 am]
This worries me about our...
by
zeljko
[
Today
at 09:25:47 am]
Lowercase Drive in LAZ 4....
by
Adie
[
Today
at 08:31:19 am]
Help with search and repa...
by
avk
[
Today
at 07:09:59 am]
Variable "xyz" doesn't no...
by
Mr.Madguy
[
Today
at 06:22:15 am]
Problem Installing CHM He...
by
Curt Carpenter
[
Today
at 03:03:11 am]
EditorMacroScript did not...
by
jamie
[
Today
at 02:55:41 am]
Mapping Images Like Odome...
by
SandyG
[November 14, 2025, 11:53:01 pm]
Programmatic filtering in...
by
jamie
[November 14, 2025, 11:27:29 pm]
Problem with Qt5 on macOS
by
zeljko
[November 14, 2025, 10:39:24 pm]
Incompatible type in MTPr...
by
LemonParty
[November 14, 2025, 06:36:59 pm]
In fpcsrc, many places ca...
by
AlexTP
[November 14, 2025, 06:15:35 pm]
Postgres, ZEOS and Visual...
by
wp
[November 14, 2025, 05:57:25 pm]
[Solved] Process exited w...
by
d7_2_laz
[November 14, 2025, 05:14:08 pm]
AVRPascal – free code edi...
by
ackarwow
[November 14, 2025, 04:21:44 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Unit StringList create and destroy [SOLVED] (Read 1191 times)
Pe3s
Hero Member
Posts: 616
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: 164
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: 616
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: 2474
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 -> FPC 3.2.2 -> Lazarus 4.0 up until Jan 2025 from then on it's both above &: KDE6/QT6 -> FPC 3.3.1 -> Lazarus 4.99
ackarwow
Full Member
Posts: 164
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: 616
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