Forum > General

Unit StringList create and destroy [SOLVED]

(1/2) > >>

Pe3s:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FImages := TStringList.Create;

ackarwow:
Hi @Pe3s

For example in this way:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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. 

Pe3s:
@ackarwow
Hello, I wonder if using constructor and destructor will also be good ?

cdbc:
Hi
Another example:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils; function GetImageList: TStringList; implementationvar  FImages: TStringList = nil; function GetImageList: TStringList;begin  if not Assigned(FImages) then FImages:= TStringList.Create;;  Result:= FImages;end; finalization  FImages.Free; end.   Regards Benny

ackarwow:

--- Quote from: Pe3s on November 27, 2024, 11:07:08 pm ---@ackarwow
Hello, I wonder if using constructor and destructor will also be good ?

--- End quote ---
Yes, you can use constuctor and destructor directly, although in case of destructor I prefer use it indirectly (via MyStrings.Free or FreeAndNil(MyStrings)).

Navigation

[0] Message Index

[#] Next page

Go to full version