The Delphi Bug List

Entry No.
556
Compiler - Shouldn't compile
Using the with statement can make read-only properties accessible for assignment
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
AbsentExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExists
Description
Reported by Michael Stratmann; checked by Reinier Sterkenburg
Consider the following code:
In some unit:
type
  TMyRecord = record
    Field1: Integer;
  end;

  TMyClass = class
  private
    FRec: TMyRecord;
  public
    property Rec: TMyRecord read FRec;
  end;
In another unit (or program):
var
  AnObj: TMyClass;
begin
  AnObj := TMyClass.Create;
  AnObj.Rec.Field1 := 1;       // compiler error: Left side cannot be assigned to
  with AnObj.Rec
  do Field1 := 1;              // compiles fine
(In Delphi 1, the compiler's behaviour is consistent: it gives an error on both assignments: Invalid variable reference.)

It is strange (wrong) that the line with the first assignment does not compile while the other with the with statement does. Either both should compile or both should not.
If the Rec property is an object (class), then both lines compile. (And if it's a static object, it behaves the same as the record case).

Latest update of this entry: 2002-02-28

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.