RAD Studio (Common)
ContentsIndex
PreviousUpNext
E2064: Left side cannot be assigned to (Delphi)

This error message is given when you try to modify a read-only object like a constant, a constant parameter, or the return value of function.

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.

The example assigns to constant parameter, to a constant, and to the result of a function call. All of these are illegal.

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 runtime*)
end.

There two ways you can solve this kind of problem: either you change the definition of whatever you are assigning to, so the assignment becomes legal, or you eliminate the assignment.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!