Thank you very much for the explanation and examples of this! I am glad you pointed out the FreeAndNill() function, I will defiantly be needing that.
James -----Original Message----- From: fpc-pascal <fpc-pascal-boun...@lists.freepascal.org> On Behalf Of Michael Van Canneyt Sent: Monday, May 20, 2019 8:03 AM To: FPC-Pascal users discussions <fpc-pascal@lists.freepascal.org> Subject: Re: [fpc-pascal] Checking if a TStringList already exists On Mon, 20 May 2019, James Richters wrote: > I’m wondering if there is a way to test if a tstringlinst has been created > yet? > > > > I looked at the documentation in these two locations: > > https://lazarus-ccr.sourceforge.io/docs/rtl/classes/tstringlist.html > > and > > https://lazarus-ccr.sourceforge.io/docs/rtl/classes/tstrings.html > > > > but they do not show things like the .Create or .Free procedures, so I > am thinking .Create and .Free are some kind of fundamental procedures that > are used by generally everything in ‘classes’ maybe so if there was some > function that would tell me if the TStringList existed, it would be in > with .Create and .Free…. But I’m not sure where that would be documented. .Create and .Free are indeed fundamental constructors and destructors for classes. They are documented in the ancestors of TString(List). A stringlist (or an object in general) exists if it is non-nil: Procedure DoSomething(var A : tStringList); begin if A=Nil then A:=TStringList.Create; end; > Also can someone explain that the purpose of .Destroy is and why I would use > .Free Vs .Destroy ? .Free Can be called on a Nil instance (if the list does not exist yet) so: A.Free; will check if A is non-nil, and calls A.Destroy if this is the case. If you call .Destroy in a nil instance, errors will occur. Note that Free or Destroy will free the instance, but will NOT set the instance pointer to Nil. So in the Above example: A:=TStringList.Create; A.Free; if A<>Nil then Writeln('A not nil'); will print A not nil. If you want to nil the pointer (recommended practice) then you should use FreeAndNil: FreeAndNil(A); Meaning that A:=TStringList.Create; FreeAndNil(A); if A<>Nil then Writeln('A not nil'); Will not print anything. Michael. _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal