Over at the question “Left side cannot be assigned to” for record type properties in Delphi, there is an answer from Toon Krijthe demonstrating how assignments to fields of a record property can be done by using properties in the declaration of the record. For easier reference, here is the code snippet published by Toon Krijthe.
type
TRec = record
private
FA : integer;
FB : string;
procedure SetA(const Value: Integer);
procedure SetB(const Value: string);
public
property A: Integer read FA write SetA;
property B: string read FB write SetB;
end;
procedure TRec.SetA(const Value: Integer);
begin
FA := Value;
end;
procedure TRec.SetB(const Value: string);
begin
FB := Value;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FRec : TRec;
public
property Rec : TRec read FRec write FRec;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Rec.A := 21;
Rec.B := 'Hi';
end;
It is clear to me why the "Left side cannot be assigned to" error is raised in the original code of vcldeveloper without the setter in the record. It is also clear to me why no error is raised for the assignment Rec.A := 21;
if a setter is defined for the property TRec.A
like in the case of the code above.
What I do not understand is why the assignment Rec.A := 21;
assigns the value 21 to the field FRec.FA
of TForm1
. I would have expected that the value is assigned to the field FA
of a local temporary copy of FRec
but not FRec.FA
itself. Could anyone please shed some light on what is happening here?
FRec.A := 21;
". It uses the setter and that isSetA
, so it callsFRec.SetA(21);
. Don't confuse the reader that much. – Thumbtack