Creating a New String List
Go Up to Working with String Lists
A string list is typically part of a component. There are times, however, when it is convenient to create independent string lists, for example to store strings for a lookup table. The way you create and manage a string list depends on whether the list is short-term (constructed, used, and destroyed in a single routine) or long-term (available until the application shuts down). Whichever type of string list you create, remember that you are responsible for freeing the list when you finish with it.
Short-term string lists
If you use a string list only for the duration of a single routine, you can create it, use it, and destroy it all in one place. This is the safest way to work with string lists.
Delphi:
Because the string-list object allocates memory for itself and its strings, you should use a try... finally block to ensure that the memory is freed even if an exception occurs.
To create a short-term string list:
- Construct the string-list object.
- In the try part of a try... finally block, use the string list.
- In the final part, free the string-list object.
C++:
Because the string-list object allocates memory for itself and its strings, you should
use a try...__finally
block to ensure that the memory is freed even if an exception occurs.
To create a short-term string list:
- Construct the string-list object.
- In the try part of a
try...__finally
block, use the string list. - In the
__finally
part, free the string-list object.
The following event handler responds to a button click by constructing a string list, using it, and then destroying it:
Delphi:
procedure TForm1.Button1Click(Sender: TObject);
var TempList: TStrings;{ declare the list }
begin
TempList := TStringList.Create;{ construct the list object }
try { use the string list }
finally TempList.Free;{ destroy the list object }
end;
end;
C++:
void __fastcall TForm1::ButtonClick1(TObject *Sender)
{
TStringList *TempList = new TStringList; // declare the list
try{
//use the string list
}
__finally{
delete TempList; // destroy the list object
}
}
Long-term string lists
If a string list must be available at any time while your application runs, construct the list at start-up and destroy it before the application terminates.
To create a long-term string list:
- In the unit file for your application's main form, add a field of type TStrings to the form's declaration.
- Write an event handler for the main form's OnCreate event that executes before the form appears. It should create a string list and assign it to the field you declared in the first step.
- Write an event handler that frees the string list for the form's OnClose event.
This example uses a long-term string list to record the user's mouse clicks on the main form, then saves the list to a file before the application terminates:
Delphi:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
ClickList: TStrings;{ declare the field }
end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
ClickList := TStringList.Create;{ construct the list }
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ClickList.SaveToFile(ChangeFileExt(Application.ExeName, '.log'));{ save the list }
ClickList.Free;{ destroy the list object }
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ClickList.Add(Format('Click at (%d, %d)', [X, Y]));{ add a string to the list }
end;
end.
C++:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ClickList = new TStringList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ClickList->SaveToFile(ChangeFileExt(Application->ExeName, ".LOG"));//Save the list
delete ClickList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TVarRec v[] = {X,Y};
ClickList->Add(Format("Click at (%d, %d)",v,ARRAYSIZE(v) - 1));//add a string to the list
}
Note: Although you can use events such as OnCreate and OnDestroy to allocate and free classes, using the constructor and destructor for a class is generally safer coding practice.