var
sl:TStringList;
begin
if .. then
sl.AddObject(sPath, TObject(4)) //給這個sPath對應的值4
else
sl.AddObject(sPath, TObject(1)); //或給這個sPath對應的值1
end;
//用到時可以
//sl.Strings[i]搭配sl.Objects[i].......(你甚至可用自定的結構喔,就像你給他更多的值,如爸爸然後加3個小孩一樣)
//傳到其它函數,可以轉換成Int64再使用
//Int64(sl.Objects[i])
以下就是一個指標結構的範例
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
SL: tstrings;
{ Private declarations }
public
{ Public declarations }
end;
type
pmyRec = ^myRec;
myRec=Record
item : String;
para1 : Integer;
para2 : Integer;
para3 : Integer;
para4 : Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Rec: pmyRec;
begin
new(Rec);
Rec.item := Edit1.text;
Rec.para1 := strtoint(Edit2.text);
Rec.para2 := strtoint(Edit3.text);
Rec.para3 := strtoint(Edit4.text);
Rec.para4 := strtoint(Edit5.text);
SL.AddObject(Rec.item, TObject(Rec));
memo1.Lines := SL;
//freemem(Rec);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SL := tstringlist.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i : integer;
begin
for i := 0 to SL.Count - 1 do
dispose(pmyRec(SL.Objects[i]));
SL.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
dispose(pmyRec(SL.Objects[1]));
sl.Delete(1);
memo1.Lines := SL;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage('sl(1)='+sl.strings[1]+', para1='+IntToStr(pmyRec(SL.Objects[1])^.para1));;
end;
end.
沒有留言:
張貼留言