Ad Space — Top Banner

E2041

Delphi Programming Language

Severity: Moderate

What Does This Error Mean?

E2041 means you tried to assign a value to, or pass as a var parameter, something that cannot be modified — like a constant, a function result, or a literal value. Only actual variables (declared with var) can appear on the left side of := or be passed as var parameters. Fix it by storing the value in a temporary variable first, or by using a const parameter instead of var.

Affected Models

  • Delphi 10.4 Sydney
  • Delphi 11 Alexandria
  • Delphi 12 Athens
  • Embarcadero RAD Studio
  • Free Pascal / Lazarus

Common Causes

  • Trying to assign to a function call result: GetValue() := 10 is not valid
  • Passing a constant value or literal as a var parameter to a function
  • Assigning to a typed constant when the typed constant is not writable
  • Trying to modify a property that has no setter defined
  • Passing a class property or computed value as a var parameter instead of a temporary variable

How to Fix It

  1. Identify what is on the left side of the := assignment or what is being passed as a var parameter — that is the operand that is not a variable.

    If it is a function call, a constant, or a literal, you cannot directly assign to it.

  2. If you need to assign to a function result, store the result in a local variable first: Temp := GetValue(); Temp := 10; — though this usually indicates a logic error.

    In most cases, E2041 on a function call means you wrote the wrong thing on the left side of :=.

  3. If the error occurs when passing a value to a var parameter, create a local variable, assign the value to it, and pass the variable instead.

    Example: Val := 42; SomeProc(Val); instead of SomeProc(42); — literals cannot be passed as var parameters.

  4. If you are writing to a property that triggers this error, check that the property has a write/setter defined in the class declaration.

    A property declared as 'property X: Integer read FX' with no 'write' clause is read-only and cannot be assigned to.

  5. For typed constants, enable writable typed constants with {$J+} or {$WRITEABLECONST ON}, but consider using a variable instead for clarity.

    Writable typed constants are a legacy feature — using a proper var declaration is the cleaner modern approach.

When to Call a Professional

This is a compiler error — no professional repair needed. Fix it by reading the error message and correcting the code. The Delphi IDE highlights the exact line causing the problem.

Frequently Asked Questions

Why can I not assign to a function result in Delphi?

A function call expression in Delphi produces a temporary value that is not stored in an addressable memory location. Assigning to it would have no effect because the temporary value disappears after the expression is evaluated. If you need to modify what a function returns, store the result in a variable and modify the variable.

What is the difference between a var and a const parameter?

A var parameter is passed by reference — the function can read and modify the caller's variable. A const parameter is passed by reference internally but the function cannot modify it — it is read-only. Use var when the function needs to update the caller's value, and const when the function only reads the value (more efficient for large types).

Can I make a Delphi property writable?

Yes — add a write clause to the property declaration: property X: Integer read FX write FX; or property X: Integer read FX write SetX; The write clause can point directly to a field or to a setter method. If you use a setter method, the method receives the new value as a parameter and can include validation logic.