Link to home
Start Free TrialLog in
Avatar of eNarc
eNarcFlag for United Kingdom of Great Britain and Northern Ireland

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
Avatar of Mahdi78
Mahdi78
Flag of Algeria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aflarin
aflarin

you can use the attached unit and its GetOrCreateStrings function like this:

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;

unit StringsByName;

interface

uses
  Classes;

function GetOrCreateStrings( const AName: string ): TStringList;

implementation

var
  FStrings: TStringList;

function GetOrCreateStrings( const AName: string ): TStringList;
var
  Index: Integer;
begin
  if FStrings.Find( AName, Index ) then
    Result:= FStrings.Objects[Index] as TStringList
  else
  begin
    Result:= TStringList.Create;
    FStrings.AddObject( AName, Result );
  end;
end;

procedure Strings_Free;
var
  i: Integer;
begin
  for i:= 0 to FStrings.Count-1 do
  begin
    FStrings.Objects[i].Free;
    FStrings.Objects[i]:= nil;
  end;
end;

initialization
  FStrings:= TStringList.Create;
  FStrings.Sorted:= True;

finalization
  Strings_Free;

end.

Open in new window

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;

Open in new window

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;
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.

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eNarc

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;