使用"for in"语句和编译器错误E2064

GJ.*_*GJ. 3 delphi delphi-2010 for-in-loop

我想用句子中的下D2010我的测试案例.

如果我想写Param.Value变量然后编译器报告错误2064,但允许从同一记录写入Param.Edit.text,为什么?

测试用例:

type
//
  TparamSet = (param_A, param_B, param_C, param_D, param_E, param_F);

  TParam = record
    Edit        :TEdit;
    Value       :integer;
  end;

var
  dtcp                  :array [TparamSet] of TParam;

procedure ResetParams;
var
  Param                 :TParam;
  A                     :Integer;
begin
  for Param in dtcp do
  begin
    Param.Edit.text:= 'Test';             //No problem
    A := Param.Value;                     //No problem
    Param.Value := 0;                     //Error: E2064 Left side cannot be assigned to;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 9

记录是值类型.该for in循环在数组中返回每个记录的副本,所以编译器错误实际上是告诉你,修改它是徒劳的.

你需要使用一个老式的for循环:

for i := low(dtcp) to high(dtcp) do
  dtcp[i].Value := 0;
Run Code Online (Sandbox Code Playgroud)