Home > Software engineering >  read write property of type record
read write property of type record

Time:02-04

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?

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

CodePudding user response:

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.

  •  Tags:  
  • Related