function test:TStringList;
var
i:integer;
v:TStringList;
begin
v:=TStringList.Create;
try
for i:=0 to 10 do
begin
v.Add(IntToStr(i));
end;
test:=v;
finally
v.Free;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items:=test;
end;
为什么这么做不可以
请问大家,函数的返回值是TStringList的时候怎么做
或者有没有其他的方法完成同样的功能.
var
i:integer;
v:TStringList;
begin
v:=TStringList.Create;
try
for i:=0 to 10 do
begin
v.Add(IntToStr(i));
end;
test:=v;
finally
v.Free;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items:=test;
end;
为什么这么做不可以
请问大家,函数的返回值是TStringList的时候怎么做
或者有没有其他的方法完成同样的功能.
v應該是個引用, 也就是一個指針的東西, 指向一片內存(v:=TStringList.Create;)但到了
finally
v.Free;
那麼內存的內容已經釋放, 不确定了所以, 你的操作可能會有問題!!修改:
//去掉 v:TStringList;
begin
result:=TStringList.Create;
var
i:integer;
begin
for i:=0 to 10 do
begin
v.Add(IntToStr(i));
end;
end;test(listBox1.Items);
function TForm1.Test(StringList:TStringList):Boolean;
var
i:integer;
v:TStringList;
begin
Result := TRUE; if assigned(StringList) = FALSE then
Result := FALSE; for i:=0 to 10 do
begin
StringList.Add(IntToStr(i));
end;
end;procedure TForm1.Button1Click(Sender: TObject);
Var
stringList: TStringList;begin
stringList:= TStringList.Create; try
if Test(stringList) = TRUE then
ListBox1.Items:=stringList; finally
stringList.Free;
end;
end;
说出了错误的原因.
也感谢xxmmmx(踢踏)
IFindit(寻找其中的乐趣)
说出了错误的原因.
也感谢xxmmmx(踢踏)
IFindit(寻找其中的乐趣)
2.请问大家,函数的返回值是TStringList的时候怎么做
或者有没有其他的方法完成同样的功能.
xxmmmx(踢踏) 我俩都不用谢, 以后你开了贴,我们会积极给你发现错误的原因(主要是回答第一个问题)!