Search code examples
delphidelphi-2010for-in-loop

Using "for in" sentence and compiler error E2064


I want to use for in sentence in my test case under D2010.

If I want to write in Param.Value variable then compiler reports error 2064, but allows to write in Param.Edit.text from the same record, why?

TEST CASE:

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;

Solution

  • Records are value types. The for in loop is returning a copy of each record in the array and so the compiler error is actually telling you that modifying it is futile.

    You'll need to use an old fashioned for loop:

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