E2129 Cannot assign to a read-only property (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Error and Warning Messages (Object Pascal) Index

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.