RAD Studio
|
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.
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. 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.
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;
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 } }
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.
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.
//--------------------------------------------------------------------------- #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
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|