Ad Space — Top Banner

E2064

Delphi Programming Language

Severity: Minor

What Does This Error Mean?

E2064 means you tried to assign a value to something that cannot be assigned to. The left side of an assignment ( := ) must be a variable or a writable property. Common causes: trying to assign to a constant, to a function call result, to a read-only property, or to a typed constant with {$J-} (write-protect) turned on. The fix is to use a variable on the left side instead of the thing that cannot be assigned.

Affected Models

  • Delphi 2
  • Delphi 7
  • Delphi XE
  • Delphi 10.x
  • Delphi 11
  • Delphi 12
  • All modern Delphi versions

Common Causes

  • Trying to assign to a const constant — constants cannot be changed after declaration
  • Trying to assign to a function or procedure result in a statement where it is used as an expression
  • Assigning to a read-only property that has only a getter (no setter)
  • Using {$J-} (write-protect typed constants) and trying to assign to a typed constant
  • Trying to assign to a loop variable inside a for loop — Delphi forbids changing the loop counter directly

How to Fix It

  1. Check what is on the left side of the := assignment. Is it a const, a read-only property, or a for loop variable?

    Hover over it in the IDE or look at its declaration. If it says 'const' or has no setter, it cannot be assigned to.

  2. If you are trying to change a constant, change its declaration to a variable (var) instead.

    Replace: const MaxItems = 100; (which cannot change) with: var MaxItems: Integer = 100; (which can be reassigned).

  3. If you are trying to set a read-only property, check the class definition. A property with only 'read' and no 'write' cannot be set directly. You will need to use a different method or work with the object differently.

    In Delphi: property Name: string read FName; — this is read-only. property Name: string read FName write SetName; — this can be written to.

  4. If you are inside a for loop and want to change the counter, you cannot. Use a while loop instead — it gives you full control over the counter variable.

    Delphi's for loop variable is read-only within the loop body. for i := 1 to 10 do — you cannot do i := 5 inside the loop.

  5. If using typed constants and getting E2064, check whether {$J-} (or {$WRITEABLECONST OFF}) is active. Use {$J+} to allow typed constants to be reassigned, or switch to var declarations.

    Typed constants (const x: Integer = 10;) can be reassigned by default, but only when {$J+} is in effect.

When to Call a Professional

E2064 is always a compile-time error you can fix yourself. The Delphi compiler tells you exactly where the problem is. Change the left side of the assignment to a variable, or add a setter to the property.

Frequently Asked Questions

What is the difference between const and var in Delphi?

const declares a named constant — a value that never changes: const Pi = 3.14159; var declares a variable — a value that can be changed at any time: var Count: Integer; Constants make code more readable and prevent accidental changes. Variables hold data that evolves during program execution.

What is a typed constant in Delphi and can it be changed?

A typed constant includes a type declaration: const MaxSize: Integer = 100; Unlike simple constants, typed constants CAN be reassigned (they are stored in a data segment, not compiled in). But only when the {$J+} or {$WRITEABLECONST ON} directive is active. With {$J-}, typed constants are write-protected and E2064 is raised if you try to change them.

Can I change a for loop variable in Delphi?

No — Delphi's for loop variable is read-only during the loop. The compiler enforces this to keep for loops predictable and optimizable. If you need to modify the counter, use a while loop instead: var i: Integer; i := 1; while i <= 10 do begin if someCondition then i := 5; Inc(i); end;