“Left side cannot be assigned to” for record type properties in Delphi

前端 未结 8 992
夕颜
夕颜 2020-12-01 16:09

I\'m curious to know why Delphi treats record type properties as read only:

  TRec = record
    A : integer;
    B : string;
  end;

  TForm1 = class(TForm)
         


        
8条回答
  •  执笔经年
    2020-12-01 16:52

    This is because property are actually complied as a function. Properties only return or set a value. It is not a reference or a pointer to the record

    so :

    Testing.TestRecord.I := 10;  // error
    

    is same as calling a function like this:

    Testing.getTestRecord().I := 10;   //error (i think)
    

    what you can do is:

    r := Testing.TestRecord;    // read
    r.I := 10;
    Testing.TestRecord := r;    //write
    

    It is a bit messy but inherent in this type of architecture.

提交回复
热议问题