Delphi Error – E2129 Cannot assign to a read-only property

Delphi Compiler Error

E2129 Cannot assign to a read-only property

Reason for the Error & Solution

The property to which you are attempting to assign a value did not specify a ‘write’ clause, thereby causing it to be a read-only property.

program Produce;

  type
    Base = class
      s : String;

      property Title : String read s;
    end;

  var
    c : Base;

  procedure DiddleTitle
  begin
    if c.Title =  then
      c.Title := 'Super Galactic Invaders with Turbo Gungla Sticks';

      (*perform other work on the c.Title*)
  end;

begin
end.
    

If a property does not specify a ‘write’ clause, it effectively becomes a read-only property; it is not possible to assign a value to a property which is read-only, thus the compiler outputs an error on the assignment to ‘c.Title’.

program Solve;

  type
    Base = class
      s : String;

      property Title : String read s;
    end;

  var
    c : Base;

  procedure DiddleTitle
    var title : String;
  begin
    title := c.Title;
    if Title =  then
      Title := 'Super Galactic Invaders with Turbo Gungla Sticks';
      (*perform other work on title*)
  end;

begin
end.
    

One solution, if you have source code, is to provide a write clause for the read-only property – of course, this could dramatically alter the semantics of the base class and should not be taken lightly. Another alternative would be to introduce an intermediate variable which would contain the value of the read-only property – it is this second alternative which is shown in the code above.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...