delphi - 使用 "for in"语句并编译错误E2064

标签 delphi delphi-2010 for-in-loop

我想在 D2010 下的测试用例中使用 for in 句子。

如果我想写入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;

最佳答案

记录是值类型。 for in 循环返回数组中每个记录的副本,因此编译器错误实际上是告诉您修改它是徒劳的。

您需要使用老式的 for 循环:

for i := low(dtcp) to high(dtcp) do
  dtcp[i].Value := 0;

关于delphi - 使用 "for in"语句并编译错误E2064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676083/

相关文章:

Delphi:类中的记录

python - 计算递归方程的列表结果

arrays - 仅在发出多个请求后调用函数

delphi - Delphi 3 Apply更新

delphi - 针对带有 TStrings 和 TStringList 的接口(interface)进行编码

delphi - 为什么 "array of string"的两个别名会受到不同的处理?

delphi - Delphi 2010有LoadTextFromFile函数吗?

performance - 如何提高delphi应用程序的启动速度?

delphi - 手动滚动 TComboBox

javascript - 使用新对象的 For/In 循环