Difference between revisions of "Property"
m |
|||
(10 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{Property}} |
− | |||
− | |||
− | |||
− | |||
− | + | ||
− | The reserved word | + | Back to [[Reserved words]]. |
+ | |||
+ | |||
+ | The [[Reserved words|reserved word]] <syntaxhighlight lang="pascal" inline>property</syntaxhighlight> is part of [[object-oriented programming]]. It can allow various levels (read, read/write etc) outside access to [[Variable|variables]] inside a [[Class|<syntaxhighlight lang="pascal" inline>class</syntaxhighlight>]]. | ||
== Example == | == Example == | ||
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang="pascal"> | ||
type | type | ||
TCar = class | TCar = class | ||
Line 20: | Line 20: | ||
// directs writes to the SetColor procedure that then changes the FColor variable. | // directs writes to the SetColor procedure that then changes the FColor variable. | ||
// Another option could be to just do "write FColor" to directly write to the FColor variable.. | // Another option could be to just do "write FColor" to directly write to the FColor variable.. | ||
− | |||
end; | end; | ||
Line 47: | Line 46: | ||
== Objects as properties == | == Objects as properties == | ||
+ | |||
You can assign objects as properties, e.g.: | You can assign objects as properties, e.g.: | ||
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang="pascal"> | ||
type | type | ||
TEngine = class | TEngine = class | ||
Line 58: | Line 59: | ||
property Engine: TEngine read FMyEngine; | property Engine: TEngine read FMyEngine; | ||
implementation | implementation | ||
− | ... | + | // ... |
− | Code in TCar that creates and frees FMyEngine as needed (e.g. in the constructor and destructor) | + | // Code in TCar that creates and frees FMyEngine as needed |
− | ... | + | // (e.g. in the constructor and destructor) |
+ | // ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
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. | 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. | ||
Line 67: | Line 69: | ||
If you wish to give calling code the responsibility of creating and destroying the object, you can also assign write access, e.g. | If you wish to give calling code the responsibility of creating and destroying the object, you can also assign write access, e.g. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="pascal"> |
... | ... | ||
public | public | ||
Line 73: | Line 75: | ||
... | ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ||
− | [ | + | == See also == |
− | + | * [https://www.freepascal.org/docs-html/ref/refse27.html property in the Official FPC documentation] |
Latest revision as of 17:19, 6 August 2022
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
Back to Reserved words.
The reserved word property
is part of object-oriented programming. It can allow various levels (read, read/write etc) outside access to variables inside a class
.
Example
type
TCar = class
private
FColor: string;
FBuildYear: integer;
procedure SetColor(CarColor: string);
public
property Color: string read FColor write SetColor; //Reads directly from the FColor variable;
// directs writes to the SetColor procedure that then changes the FColor variable.
// Another option could be to just do "write FColor" to directly write to the FColor variable..
end;
procedure TCar.SetColor(CarColor: string);
begin
//we can do other things here, too, such as housekeeping, notifying others of changes,
//validating against other variables, logging, etc -e.g.
if CarColor='Pink' then
ShowMessage('Note: pink is not a very common color for a car.');
FColor:=CarColor;
end;
procedure PropertyExample(); //shows how to set properties and read them
var
MyCar: TCar; // Will hold a TCar instance; used in the example subAuto
begin
MyCar := TCar.Create; // Create the object
try
MyCar.Color:='Green'; // Sets the color property... which calls the setcolor function... which sets the object's FColor variable
showmessage(MyCar.Color); // Now read the object property again... should show Green
finally
MyCar.Free; // Free the memory for the object again, regardless of what errors occurred in the rest of the code
end;
end;
Objects as properties
You can assign objects as properties, e.g.:
type
TEngine = class
...
TCar = class
private
FMyEngine: TEngine;
public
property Engine: TEngine read FMyEngine;
implementation
// ...
// Code in TCar that creates and frees FMyEngine as needed
// (e.g. in the constructor and destructor)
// ...
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.
However, it can use this read access to access the properties of the FMyEngine object and change those.
If you wish to give calling code the responsibility of creating and destroying the object, you can also assign write access, e.g.
...
public
property Engine: TEngine read FMyEngine write FMyEngine;
...