Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

read write property of type record

Is their any drawback to declare a property of type record ?

  TMyObject = class(TObject)
  private
    FSomeRecord: TMyRecord;
  public
    property SomeRecord: TMyRecord read FSomeRecord write FSomeRecord;
  end;

How the instruction Myobject.SomeRecord.xxx := yyy will work under the hood (I think this can not work actually) ?

If it’s can not work, how to do with record property? is it simply better to avoid it and declare TMyobject like below?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  TMyObject = class(TObject)
  public
    SomeRecord: TMyRecord;
  end;

>Solution :

You are correct that Myobject.SomeRecord.xxx := yyy will not work the way you want. It will invoke the property getter, returning a copy of the record, and then you would be updating the xxx field of the copy, not the original. Essentially, the generated code would act like this:

var tmp: TMyRecord;
tmp = Myobject.FSomeRecord;
tmp.xxx := yyy;

Unless you need RTTI for the property, there is no good reason to declare a read+write property that simply accesses a field directly. Just expose public access to the field instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading