Problems encountered after a "struct" is regarded as a property

Source: Internet
Author: User
When I regard a "struct" as an attribute in the class, I can directly read the struct members in practice, but cannot write them directly...

The following is a small exercise:

Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs, VCL. struct; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; Procedure button1click (Sender: tobject ); procedure button2click (Sender: tobject); Procedure button4click (Sender: tobject); Procedure button3click (Sender: tobject); Procedure button5click (Sender: tobject); end; tmyclass = Class strict private FPOs: tpoint; Procedure setpos (const value: tpoint); Public Property pos: tpoint read FPOs write setpos; // attribute POS corresponds to a point structure end; vaR form1: tform1; implementation {$ R *. DFM} {tmyclass} procedure tmyclass. setpos (const value: tpoint); begin FPOs: = value; end; {test} procedure tform1.button1click (Sender: tobject); var OBJ: tmyclass; begin OBJ: = tmyclass. create; showmessagefmt ('% d, % d', [obj. POS. x, obj. POS. y]); // you can directly access the elements in the structure. // obj. POS. x: = 11; // but the element in the structure cannot be directly assigned a value. // obj. POS. y: = 22; obj. free; end; // change to procedure tform1.button2click (Sender: tobject); var OBJ: tmyclass; begin OBJ: = tmyclass. create; obj. POs: = point (22, 33); // showmessagefmt ('% d, % d', [obj. POS. x, obj. POS. y]); obj. free; end; // work und 2. Procedure tform1.button3click (Sender: tobject); var OBJ: tmyclass; Pt: tpoint; begin OBJ: = tmyclass. create; PT. x: = 33; PT. y: = 44; obj. POs: = pt; showmessagefmt ('% d, % d', [obj. POS. x, obj. POS. y]); obj. free; end; // work 3 (if the get attribute is not a method) Procedure tform1.button4click (Sender: tobject); var OBJ: tmyclass; P: Ppoint; begin OBJ: = tmyclass. create; P: = ADDR (obj. pos); p. x: = 44; p. y: = 55; showmessagefmt ('% d, % d', [obj. POS. x, obj. POS. y]); obj. free; end; // work und 4 (if the get attribute is not a method) Procedure tform1.button5click (Sender: tobject); var OBJ: tmyclass; begin OBJ: = tmyclass. create; Ppoint (ADDR (obj. pos )). x: = 55; Ppoint (ADDR (obj. pos )). y: = 66; showmessagefmt ('% d, % d', [obj. POS. x, obj. POS. y]); obj. free; end.
Exercise 2:
Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject); end; tmyclass = class private FPOs: tpoint; function getpos: tpoint; Procedure setpos (const value: tpoint); function getxy (const index: integer): integer; Procedure setxy (const index, value: integer); Public Property pos: tpoint read getpos write setpos; property X: integer index 0 read getxy write setxy; property Y: integer Index 1 read getxy write setxy; end; var form1: tform1; implementation {$ R *. DFM} {tmyclass} function tmyclass. getpos: tpoint; begin result: = FPOs; end; Procedure tmyclass. setpos (const value: tpoint); begin FPOs: = value; end; function tmyclass. getxy (const index: integer): integer; begin result: = 0; case index of 0: Result: = FPOs. x; 1: Result: = FPOs. y; end; Procedure tmyclass. setxy (const index, value: integer); begin case index of 0: FPOs. x: = value; 1: FPOs. y: = value; end; {test} procedure tform1.formcreate (Sender: tobject); var OBJ: tmyclass; begin OBJ: = tmyclass. create; obj. x: = 11; obj. y: = 22; showmessagefmt ('% d, % d', [obj. POS. x, obj. POS. y]); obj. free; end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.