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 E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...