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
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Conditional variable defi...
by
440bx
[
Today
at 11:07:58 am]
Painting on a control - 3...
by
majolika
[
Today
at 11:06:26 am]
Amigo programming languag...
by
paxscript
[
Today
at 10:05:39 am]
Result not being set in c...
by
Martin_fr
[
Today
at 09:51:09 am]
Drawing with psInsidefram...
by
TRon
[
Today
at 09:38:56 am]
FPHTTPClient and SSL not ...
by
geraldholdsworth
[
Today
at 09:17:45 am]
Exclude Jedi Code Formatt...
by
JuhaManninen
[
Today
at 06:13:54 am]
UK's Online Safety Act
by
Joanna from IRC
[
Today
at 01:38:37 am]
Return nil/null of data t...
by
VisualLab
[
Today
at 12:36:54 am]
How C++ class multiple in...
by
VisualLab
[
Today
at 12:24:05 am]
Preparing FPC 3.2.4, poin...
by
PascalDragon
[January 23, 2025, 10:34:10 pm]
Generic function speciali...
by
PascalDragon
[January 23, 2025, 10:17:13 pm]
Resolving procedure symbo...
by
440bx
[January 23, 2025, 10:16:17 pm]
Assembler window colors
by
Kornelius
[January 23, 2025, 10:11:46 pm]
laz.virtualtrees.pas not ...
by
kapibara
[January 23, 2025, 10:07:25 pm]
Does unit recompilation r...
by
Fred vS
[January 23, 2025, 10:00:54 pm]
Out ...: Array of Somethi...
by
440bx
[January 23, 2025, 09:08:57 pm]
Generic methods inside a ...
by
PascalDragon
[January 23, 2025, 09:03:58 pm]
Compiling 2 dependent pac...
by
Sergey Tkachenko
[January 23, 2025, 08:36:59 pm]
[SOLVED] Application.Proc...
by
erol
[January 23, 2025, 07:46:48 pm]
Timer difference in Linux...
by
Thaddy
[January 23, 2025, 06:51:31 pm]
مشكلة في التقارير
by
nouzi
[January 23, 2025, 06:28:58 pm]
Migrate vb.net to Lazarus...
by
Packs
[January 23, 2025, 06:16:35 pm]
Looking For Lazarus Tutor...
by
NobleStar
[January 23, 2025, 04:13:44 pm]
Getting format error load...
by
n7800
[January 23, 2025, 04:04:34 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Unit StringList create and destroy [SOLVED] (Read 752 times)
Pe3s
Hero Member
Posts: 590
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: 128
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: 590
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: 1786
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 -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0
ackarwow
Full Member
Posts: 128
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: 590
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