Results 1 to 4 of 4

Thread: Record object benaderen via een property

  1. #1
    John Kuiper
    Join Date
    Apr 2007
    Location
    Almere
    Posts
    8,747

    Record object benaderen via een property

    Ik heb het volgende gegeven:
    Delphi Code:
    1. type
    2.   TMyRecordTest = record
    3.     name : string;
    4.     value : integer;
    5.   end;
    6.  
    7.   TMyTest = class
    8.       fMyRecordtest : TMyRecordTest;
    9.     public
    10.       property MyRecordtest : TMyRecordTest read fMyRecordtest write fMyRecordtest;
    11.   end;
    12.  
    13.   TForm1 = class(TForm)
    14.     procedure FormShow(Sender: TObject);
    15.   private
    16.     { Private declarations }
    17.   public
    18.     { Public declarations }
    19.   end;
    20.  
    21. var
    22.   Form1: TForm1;
    23.  
    24. implementation
    25.  
    26. {$R *.dfm}
    27.  
    28. procedure TForm1.FormShow(Sender: TObject);
    29. var test : TMyTest;
    30. begin
    31.   test := TMyTest.Create;
    32.   test.MyRecordtest.name := 'john';
    33.   test.MyRecordtest.value := 50;
    34.   test.Free;
    35. end;
    Deze melding geeft mij door de compiler : [dcc32 Error] Unit1.pas(42): E2064 Left side cannot be assigned to
    Als ik de record object rechtstreeks wil benaderen met fMyRecordtest, dan mag het weer wel.

    Heeft dat te maken met de property MyRecordtest, die ik gemaakt heb om het record object te kunnen benaderen. Het lijkt er op dat hij iets wilt wegschrijven naar TMyRecordtest en dat is een record object en geen variabele. Maar de property zorgt er toch voor dat de variabele fMyRecordtest wordt gelezen en dat met de verdere notatie na de punt de juiste variabele wordt gesprongen?

    Deze zelfde test werkt prima als TMyRecordtest wordt omgezet naar een class object.
    Delphi is great. Lazarus is more powerfull

  2. #2
    Senior Member
    Join Date
    Aug 2004
    Location
    Rotterdam
    Posts
    151
    Read MyRecordTest geeft een kopie terug van het record.
    Daarom mag/kan je niet schrijven naar de sub waardes. Want die komen "nergens" terecht.

  3. #3
    *+E13818MU01F0F* Norrit's Avatar
    Join Date
    Aug 2001
    Location
    Landgraaf
    Posts
    967
    https://stackoverflow.com/questions/...ties-in-delphi

    en dan de quote
    Since "Rec" is a property, the compiler treats it a little differently because it has to first evaluate the "read" of the property decl. Consider this, which is semantically equivalent to your example:

    ...
    property Rec: TRec read GetRec write FRec;
    ...
    If you look at it like this, you can see that the first reference to "Rec" (before the dot '.'), has to call GetRec, which will create a temporary local copy of Rec. These temporaries are by design "read-only." This is what you're running into.
    Objective reality is a delirium caused by lack of alcohol in blood

  4. #4
    John Kuiper
    Join Date
    Apr 2007
    Location
    Almere
    Posts
    8,747
    Oke. Zo heb ik het niet bekeken.
    Delphi is great. Lazarus is more powerfull

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •