eNarc
asked on
How to Check if a StringList has been Activated/Created
Hi, is there anyway to see if a Stringlist has been activated/created and if not create one with a selective name and if its already created, use that stringlist.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try the code snippet;
Var nString: TStringList;
procedure TForm1.FormCreate(Sender: TObject);
begin
...
//Create the StringList
nString := TStringList.Create;
nString.Duplicates := dupIgnore;
...
end;
//String will be added if not exist in the list
procedure TForm1.Button1Click(Sender: TObject);
begin
if nString.IndexOf('ThisString') = -1
then
nString.Add('ThisString');
...
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
nString.Free;
...
end;
sometimes i have the need to use a implementation variable
a stringlist or other object
this is the code and function i use for this
the benefit is that the object only gets initialised when used
and free at unit finalization
in any code using that unit you don't need to bother about creating or freeing the object
procedure TForm1.Test;
begin
ShowMessage(Obj.ToString);
end;
a stringlist or other object
this is the code and function i use for this
the benefit is that the object only gets initialised when used
and free at unit finalization
in any code using that unit you don't need to bother about creating or freeing the object
procedure TForm1.Test;
begin
ShowMessage(Obj.ToString);
end;
interface
function Obj: TObject;
implementation
var
mObject: TObject;
function Obj: TObject;
begin
if not Assigned(mObject) then
begin
mObject := TObject.Create;
// further initialisation
end;
Result := mObject;
end;
procedure FinishObject;
begin
FreeAndNil(mObject);
end;
initialization
finalization
FinishObject;
end.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
var
Form1: TForm1;
sl:tstringlist;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender : TObject);
begin
sl:=tstringlist.Create;
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
if not Assigned(SL) then
showmessage('not')
else
showmessage('yea')
end;
procedure TForm1.Button3Click(Sender : TObject);
begin
freeandnil(sl);
end;
Form1: TForm1;
sl:tstringlist;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender
begin
sl:=tstringlist.Create;
end;
procedure TForm1.Button2Click(Sender
begin
if not Assigned(SL) then
showmessage('not')
else
showmessage('yea')
end;
procedure TForm1.Button3Click(Sender
begin
freeandnil(sl);
end;
var
StrList, StrList2: TStringList;
begin
StrList:= GetOrCreateStrings( 'YourListName' );
StrList.Add( 'Some string 1' );
StrList.Add( 'Some string 2' );
// don't need to destroy StrList
StrList2:= GetOrCreateStrings( 'YourListName' );
ShowMessage( StrList2[0] ); // Some string 1
ShowMessage( StrList2[1] ); // Some string 2
// don't need to destroy StrList2 too
end;
Open in new window