delphi动态数组问题?

来源:百度知道 编辑:UC知道 时间:2024/05/16 15:11:18
我写的程序为:
var
I:integer;
begin
I:=ComboBox1.Items.Count-1;
SetLength(str,I);
I:=0;
for I:=0 to ComboBox1.Items.Count-1 do
begin
str[i]:=ComboBox1.Items[I];
end;
为什么运行之后出错

var
I:integer;
str:array of String;
begin
I:=ComboBox1.Items.Count;
showmessage(IntToStr(i));
SetLength(str,I);
I:=0;
for I:=0 to ComboBox1.Items.Count-1 do
begin
str[i]:=ComboBox1.Items[I];
showmessage(str[i]);
end;
end;
错误1:str未声明
2:动态空间长度有问题I:=ComboBox1.Items.Count;

var
I:integer;
str: Tstringlist;
begin
str: Tstringlist.create;
for I:=0 to ComboBox1.Items.Count-1 do
begin
str.add(ComboBox1.Items[I]);
end;
//最后别忘了释放str: str.free;