怎样将数组作为参数传给“线程”!(100分)

徐涛

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用多线程来检索数组中的值。
其中我的定义如下:
type
xt_hm = class(TThread)
private

Ta:array[1..551]Of string;
protected
procedure Execute;
override;
procedure disloop;
public
constructor create(var Tt:array of string);
end;

implementation
uses u_xt_2;
constructor xt_hm.create(var tt:array of string);
begin
inherited create(false);
Aedit:=edit;
freeonterminate:=true;
ta:=tt;
//问题就在这,系统总是提示
[Error] U_thread1.pas(37): Incompatible types。
end;
 
你是AEdit和Edit分别是什么?(类型)
 
type
TMyArray=array [0..100] string;
xt_hm = class(TThread)
private
Ta:TMyArray;
protected
procedure Execute;
override;
procedure disloop;
public
constructor create(var Tt:TMyArray);
end;

implementation
uses u_xt_2;
constructor xt_hm.create(var tt:TMyArray);
begin
inherited create(false);
Aedit:=edit;
freeonterminate:=true;
ta:=tt;
end;
 
参数中的数组即使定义的一模一样,也会报错,所以得单独定义一个类型,
一般来说,复杂类型最好能单独定义,比如象上面的数组,集合,
过程, 等等。这样传参时既方便有不发生错误。
 
这种错误是由于ta为静态数组,而tt为动态数组,
只须
ta:array of string;
...
setlength(ta,high(tt)+1);
ta:=tt;
搞掂

 
你可以在线程中定义已数据类型
type
TPtr=^TmyArray;
TmyArray=Array[0..100] of string;
....
..Thread=class(TThread)
....
Ptr:Tptr;
....
然后在Constrctor ..Thread.createIT(...;Myarray:TmyArray;..);中
让 Ptr=@Myarray;
后即可使用ptr访问数组.
 
多人接受答案了。
 
顶部