In the answer by David Heffernan, he use assignment to read-only properties (see complete program example). There are two property: RecDirect and RecFunction. Both are read-only.
My question is why would one write to something that is read-only? Also I think properties are very flexible and useful. So why would one use properties and after all refer to corresponding underlying fields?
CodePudding user response:
In the linked answer David did not write to read only property.
The Obj.RecDirect.A := 21;
dos not write to a read only property.
What this does is to use RedDirect
property first to retrieve reference to a Record object.
And then it uses A
property of that referenced Record to write a value of 21 to FA
field of that referenced Record. And if you take a look at property A
definition in that record you can see that is defined as Read/Write property.
CodePudding user response:
with readonly you can protect your class(record) to contain meaningless or false data which may lead to access violations or wrong performance.... Don't let external users of your class write garbage data to the class
Even better use properties and getter and setter function to make sure only good data is in your class.
This is not a Delphi question only -- it's valid for all OOP languages