Delphi Compiler Error
E2064 Left side cannot be assigned to
Reason for the Error & Solution
This error message is given when you try to modify a read-only object such as a constant, a constant parameter, the return value of function, read-only properties, or fields of read-only properties.
There are two ways you can solve this kind of problem:
- Change the definition of whatever you are assigning to, so the assignment becomes legal.
- Eliminate the assignment altogether.
Examples
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 previous example assigns to a constant parameter, to a constant, and to the result of a function call. All of these are illegal. The following example illustrates how to fix these problems.
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.
In Delphi 2010 and above, E2064 is also emitted by the compiler whenever you try to assign a value to a member of a record exposed by a read-only property. Consider the following record type:
TCustomer = record Age: Integer; Name: String; end;
that is exposed by a read-only property in a class:
TExposing = class ... CurrentCustomer: TCustomer read SomeInternalField; ... end;
Assigning a value to either the CurrentCustomer property or to a member of the record exposed by the CurrentCustomer property results in the compiler emitting this E2064 error.