下面这样写是不会错的:
procedure TForm1.Button1Click(Sender: TObject); var arr1,arr2 : array of integer; i : integer; sTmp : string; begin for i := 0 to Length(arr1)-1 do arr1[i] := i + 1; Memo1.Lines.Add(ArrayToString(arr1)); arr2 := Copy(arr1, 0, 5); Memo1.Lines.Add(ArrayToString(arr2)); end;
如果把两个数组分两行定义,就会报错,如下:
procedure TForm1.Button1Click(Sender: TObject); var arr1 : array of integer; arr2 : array of integer; i : integer; sTmp : string; begin for i := 0 to Length(arr1)-1 do arr1[i] := i + 1; Memo1.Lines.Add(ArrayToString(arr1)); arr2 := Copy(arr1, 0, 5); Memo1.Lines.Add(ArrayToString(arr2)); end;
报错:[dcc32 Error] Unit1.pas(63): E2008 Incompatible types
难道分两行定义的动态数组,就不是同一个类型了吗?
或者,这是Delphi编译器的一个BUG?
如果自定义一个类型,就不会报错了,如下:
type IntArray = array of integer; procedure TForm1.Button1Click(Sender: TObject); var arr1 : IntArray; arr2 : IntArray; i : integer; sTmp : string; begin for i := 0 to Length(arr1)-1 do arr1[i] := i + 1; Memo1.Lines.Add(ArrayToString(arr1)); arr2 := Copy(arr1, 0, 5); Memo1.Lines.Add(ArrayToString(arr2)); end;
标签:数组,Lines,Add,arr2,arr1,integer,Copy,BUG,Memo1 From: https://www.cnblogs.com/CipherLab/p/17927724.html