//定义
PArrFields = ^TArrFields;
TArrFields = record
F1: string;
F2: string;
end;ArrFields: array of TArrFields;const
AFieldS: array[0..1] of TArrFields = ((F1:'FGoodsNo';F2:'FGoodsNo'),(F1:'FPrice';F2:'FInPri'));
begin
SetFields(@AFieldS);
end;procedure SetFields(arr: PArrFields);
begin
ArrFields := @arr; //这边要给record数组赋值就出错
end;我的目的是用自定义的record数组做参数,并赋值给数组ArrFields,需要使用时就这样取出值,
for i := Low(ArrFields) to High(ArrFields) do
begin
showmessage(ArrFields[i].F1).AsString);
end;
但是赋值的时候就会出错,请问大虾们这是怎么回事?先谢谢了
PArrFields = ^TArrFields;
TArrFields = record
F1: string;
F2: string;
end;ArrFields: array of TArrFields;const
AFieldS: array[0..1] of TArrFields = ((F1:'FGoodsNo';F2:'FGoodsNo'),(F1:'FPrice';F2:'FInPri'));
begin
SetFields(@AFieldS);
end;procedure SetFields(arr: PArrFields);
begin
ArrFields := @arr; //这边要给record数组赋值就出错
end;我的目的是用自定义的record数组做参数,并赋值给数组ArrFields,需要使用时就这样取出值,
for i := Low(ArrFields) to High(ArrFields) do
begin
showmessage(ArrFields[i].F1).AsString);
end;
但是赋值的时候就会出错,请问大虾们这是怎么回事?先谢谢了
begin
ArrFields := @arr; //这边要给record数组赋值就出错
end;procedure SetFields(arr: PArrFields);
begin
ArrFields :=arr;
end;
Move(AFields[0],ArrFields[0],SizeOf(Tarrfields)*2);
showmessage(ArrFields[0].F1);
Move(arr[0],ArrFields[0],SizeOf(Tarrfields)*2);//编译报错[Error] U_GoodsSel.pas(188): Array type required
SetFields 里面没有AFields参数
SetLength(ArrFields,2);
Move(AFields[0],ArrFields[0],SizeOf(Tarrfields)*2);
showmessage(ArrFields[0].F1);不在SetFields 函数里调用了
一个静态的AFieldS: array[0..1] of TArrFields传给另一个动态的
ArrFields: array of TArrFields;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
PArrFields = ^TArrFields;
TArrFields = record
F1: string;
F2: string;
end;
var
Form1: TForm1;
ArrFields: array of TArrFields;
AFieldS: array[0..1] of TArrFields = ((F1: 'FGoodsNo'; F2: 'FGoodsNo'), (F1: 'FPrice'; F2: 'FInPri'));
implementation{$R *.dfm}
//数组本来就是指针呢。procedure SetFields(arr: PArrFields);
begin
ArrFields := @arr; //这边要给record数组赋值就出错
end;procedure TForm1.Button1Click(Sender: TObject);
begin SetLength(ArrFields, 2);
Move(AFields[0], ArrFields[0], SizeOf(Tarrfields) * 2); showmessage(ArrFields[0].F1);
end;end.
这样确实可以,但是能否通过参数传AFieldS: array[0..1] of TArrFields呢?
http://topic.csdn.net/u/20081128/14/1558d17f-dc24-41a2-98ec-df43882a3fa6.html?1076911347
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
PArrFields = ^TArrFields;
TArrFields = record
F1: string[20];
F2: string[20];
end; taaa=array[0..1] of TArrFields;var
Form1: TForm1;
ArrFields:array of TArrFields;
AFieldS: Taaa = ((F1: 'FGoodsNo'; F2: 'FGoodsNo'), (F1: 'FPrice'; F2: 'FInPri'));
implementation{$R *.dfm}
//数组本来就是指针呢。procedure SetFields(arr: taaa);
begin //ArrFields :=@arr; //这边要给record数组赋值就出错 showmessage(ArrFields[0].F1);
Move(AFields[0], ArrFields[0], SizeOf(Tarrfields) * 2);
end;procedure TForm1.Button1Click(Sender: TObject);
begin SetLength(ArrFields, 2);
FillChar(ArrFields,0,SizeOf(Tarrfields)*2);
{ Move(AFields[0], ArrFields[0], SizeOf(Tarrfields) * 2);
}
SetFields(afields);
showmessage(ArrFields[0].F1);
end;end.
procedure SetFields(arr: PArrFields);
begin
ArrFields :=arr;
end;
begin //ArrFields :=@arr; //这边要给record数组赋值就出错 showmessage(ArrFields[0].F1);
Move(AFields[0], ArrFields[0], SizeOf(Tarrfields) * 2);
end;
这句的arr参数完全没用了
procedure SetFields(arr: array of TArrFields);
begin
SetLength(ArrFields, 2);
Move(arr[0], ArrFields[0], SizeOf(Tarrfields) * 2);
end;
就可以了,谢谢lanyun2