24 Votes

Delphi: Check whether object or variable exists

Tip by Delphian | 2013-02-15 at 12:51

Sometimes you have to test whether an object or variable in Delphi exists at all or has already been initialized. How you do it, I show you in this tip.

We are using assigned() for this purpose. Assigned() returns true, if the object is existing and false if not. Let's look at an example:

var
  sl: TStringList;

...

if assigned(sl) then sl.free;

...

if assigned(sl)=false then sl:=TStringList.Create();

In this example, we only free the StringList in the case, the StringList exists and we are creating a new StringList only in the case, the StringList is not existing.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.