1. Computing

in Delphi TIPS :: According to the "Proper using of a Record Field/Property in a Class" poll, only 20+% of Delphi developers know what usage of a Record Delphi type as a Property in a Class declaration will produce a compile time error while trying to assign a value to a record's field.

If you expose a Record type variable as a property of a Delphi class, trying to assign a value to one of record fields will result in "left side cannot be assigned to", as in:

objectInstance.RecordAsProperty.Field := SomeValue; // erorr!

Read the full article to find out how to why this does not compile and how to fix the left side cannot be assigned to error.

Related: Understanding the Record Type in Delphi | Learning Object Oriented Programming with Delphi | Statements, Properties, Variables for Beginners

Comments
December 3, 2008 at 6:45 pm
(1) Sam says:

Besides that… for beginners, make sure the left side is not declared as a constant!

October 15, 2009 at 11:00 am
(2) Warren P. says:

The 2010 compiler was fixed up to correctly report errors in some cases around left-side-cannot-be assigned, and this fix broke some badly written code that users had already compiled just fine in earlier versions.

Here’s some sample code:

type
TRec = record
C:Integer;
end;
TClass1 = class
protected
FB:TRec;
public
property B:TRec read FB;
end;
….
var

implementation
….
procedure TForm1.Button1Click(Sender: TObject);
var
A:TClass1;
begin

A := TClass1.Create;

A.FB.C := 1; // no problem.

A.B.C := 1; // Left side cannot be assigned
// (All delphi versions report this error)

// Only delphi 2010 reports this as
// “Left side cannot be
//assigned” error:
with A.B do begin
C := 1;
end;

end;

…code snippet ends…

Warren

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.