Delphi – E2064 Left side cannot be assigned to

When you try to change a read-only object like a constant, a constant argument, the return value of a function, read-only properties, or fields of read-only properties in your delphi program, you’ll see this error message.

You can tackle this problem in one of two ways:

So that the assignment is legal, change the definition of whatever you’re assigning to.
Remove the task entirely from the equation.

program Produce;

const
  c = 1;

procedure p(const s: string);
begin
  s := 'changed';            (*<-- Error message here*)
end;

function f: PChar;
begin
  f := 'Hello';              (*This is fine - we are setting the return value*)
end;

begin
  c := 2;                    (*<-- Error message here*)
  f := 'h';                  (*<-- Error message here*)
end.

In the above example, a constant parameter, a constant, and the result of a function call are all assigned. All of this is against the law. The example below demonstrates how to resolve these issues.

program Solve;

var
  c : Integer = 1;           (*Use an initialized variable*)

procedure p(var s: string);
begin
  s := 'changed';            (*Use variable parameter*)
end;

function f: PChar;
begin
  f := 'Hello';              (*This is fine - we are setting the return value*)
end;

begin
  c := 2;
  f^ := 'h';                 (*This compiles, but will crash at run time*)
end.

E2064 is also produced by the compiler in Delphi 2010 and later when you try to assign a value to a member of a record exposed through a read-only property. Consider the following type of record:

TCustomer = record
  Age: Integer;
  Name: String;
end;

that a read-only property in a class exposes:

TExposing = class
...
  CurrentCustomer: TCustomer read SomeInternalField;
...
end;

The compiler generates this E2064 error when a value is assigned to either the CurrentCustomer property or a member of the record exposed by the CurrentCustomer property.