Property: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Property
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Back to [[Reserved words]]. | |
| 2 | + | ||
| 3 | + | The [[Reserved words|reserved word]] `property` is part of [[object-oriented programming]]. It can allow various levels (read, read/write etc) outside access to [[Variable|variables]] inside a [[Class|class]]. | |
| 4 | + | ||
| 5 | + | ## Example | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | type | |
| 9 | + | TCar = class | |
| 10 | + | private | |
| 11 | + | FColor: string; | |
| 12 | + | FBuildYear: integer; | |
| 13 | + | procedure SetColor(CarColor: string); | |
| 14 | + | public | |
| 15 | + | property Color: string read FColor write SetColor; //Reads directly from the FColor variable; | |
| 16 | + | // directs writes to the SetColor procedure that then changes the FColor variable. | |
| 17 | + | // Another option could be to just do "write FColor" to directly write to the FColor variable.. | |
| 18 | + | end; | |
| 19 | + | ||
| 20 | + | procedure TCar.SetColor(CarColor: string); | |
| 21 | + | begin | |
| 22 | + | //we can do other things here, too, such as housekeeping, notifying others of changes, | |
| 23 | + | //validating against other variables, logging, etc -e.g. | |
| 24 | + | if CarColor='Pink' then | |
| 25 | + | ShowMessage('Note: pink is not a very common color for a car.'); | |
| 26 | + | FColor:=CarColor; | |
| 27 | + | end; | |
| 28 | + | ||
| 29 | + | procedure PropertyExample(); //shows how to set properties and read them | |
| 30 | + | var | |
| 31 | + | MyCar: TCar; // Will hold a TCar instance; used in the example subAuto | |
| 32 | + | begin | |
| 33 | + | MyCar := TCar.Create; // Create the object | |
| 34 | + | try | |
| 35 | + | MyCar.Color:='Green'; // Sets the color property... which calls the setcolor function... which sets the object's FColor variable | |
| 36 | + | showmessage(MyCar.Color); // Now read the object property again... should show Green | |
| 37 | + | finally | |
| 38 | + | MyCar.Free; // Free the memory for the object again, regardless of what errors occurred in the rest of the code | |
| 39 | + | end; | |
| 40 | + | end; | |
| 41 | + | ``` | |
| 42 | + | ||
| 43 | + | ## Objects as properties | |
| 44 | + | ||
| 45 | + | You can assign objects as properties, e.g.: | |
| 46 | + | ||
| 47 | + | ``` pascal | |
| 48 | + | type | |
| 49 | + | TEngine = class | |
| 50 | + | ... | |
| 51 | + | TCar = class | |
| 52 | + | private | |
| 53 | + | FMyEngine: TEngine; | |
| 54 | + | public | |
| 55 | + | property Engine: TEngine read FMyEngine; | |
| 56 | + | implementation | |
| 57 | + | // ... | |
| 58 | + | // Code in TCar that creates and frees FMyEngine as needed | |
| 59 | + | // (e.g. in the constructor and destructor) | |
| 60 | + | // ... | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | Read access here means something that may not be obvious at first glance. Outside code has read only access to the *pointer*/reference to the TEngine object in question, which means it cannot create or destroy the object. | |
| 64 | + | ||
| 65 | + | However, it can use this read access to access the properties of the FMyEngine object and change those. | |
| 66 | + | ||
| 67 | + | If you wish to give calling code the responsibility of creating and destroying the object, you can also assign write access, e.g. | |
| 68 | + | ||
| 69 | + | ``` pascal | |
| 70 | + | ... | |
| 71 | + | public | |
| 72 | + | property Engine: TEngine read FMyEngine write FMyEngine; | |
| 73 | + | ... | |
| 74 | + | ``` | |
| 75 | + | ||
| 76 | + | ## See also | |
| 77 | + | ||
| 78 | + | - [property in the Official FPC documentation](https://www.freepascal.org/docs-html/ref/refse27.html) | |
| 79 | + | ||
| 80 | + | --- | |
| 81 | + | *From the Free Pascal wiki, page "Property", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
