Last Updated:

Fields, Properties, and Methods | Delphi | Tutorial

Class fields are variables declared within a class. They are designed to store data while an instance of a class (object) is running. There are no restrictions on the type of fields in the class. In a class description, fields must precede methods and properties. Typically, fields are used to enable operations within a class.

When declaring field names, it is customary to add a capital letter F to the name. For example, FSomeField

So, the fields are intended to be used within a class. However, the class must interact in some way with other classes or programmatic elements of the application. In the vast majority of cases, a class must perform certain actions with some data and present the result.

Properties are used to retrieve and transmit data in the class. The reserved word property is used to declare properties in the class.

Properties are attributes that make up an object's personality and help describe it. For example, a regular button in an application window has properties such as color, dimensions, and position. For an instance of the button class, the values of these attributes are set using properties, special variables defined by the property keyword. The color can be set by the Color property, the dimensions by the Width and Height properties, and so on.

Because a property enables communication with the external environment, special class methods are used to access its value. Therefore, a property is typically defined by three elements: a field and two methods that read/write it:

TAnObject = class(TObject)
function GetColor: TSomeType;
procedure SetColor(ANewValue: TSomeType);
property AColor: TSomeType read GetColor write SetColor;

In this example, the value of the AColor property is accessed through calls to the GetColor and SetColor methods. However, there is no need to refer to these methods explicitly: it is enough to write:

AnObject.AColor: = AValue;
AVariable: = AnObject.AColor;

And the compiler itself translates the access to the AColor property into calls to the Getcolor or Setcolor methods. On the surface, the property looks exactly like an ordinary field, but behind any appeal to it there may be the actions you need. For example, if you have an object that is a square on the screen, and you set its color property to white, an immediate redraw occurs that brings the actual color on the screen into line with the property value. This operation is performed by the method that is associated with setting the value of the color property.

The methods that are part of the properties can check the set value for falling within the valid range of values and call other procedures that depend on the changes that you make. If you do not need special read and/or write procedures, you can use field names instead of method names. Consider the following design:

TPropObject = class(TObject)
FValue: TSomeType;
procedure DoSomething;
function Correct(AValue: Integer):boolean;
procedure SetValue(NewValue: Integer);
property AValue: Integer read FValue write SetValue;
procedure TPropObject.SetValue(NewValue: Integer);
if (NewValueoFValue) and Correct(NewValue) then EValue: = NewValue;

In this example, reading the value of the AValue property is simply reading the rvalue field. However, when assigning a value inside SetValue, two methods are called at once.

If the property is only to be read or written, the corresponding method may be present in its description:

TAnObject = class(TObject)
property AProperty: TSomeType read GetValue;

In this example, outside the object, the property value can only be read; attempting to set the AProperty property to a value will cause a compilation error.

To set a property to a default value, use the default keyword:

property Visible: boolean read FVisible write SetVisible default True;

This means that when the program starts, the property will be set to True by the compiler.

The property can also be vector; in this case, it looks like an array:

property APoints[Index: Integer]:TPoint read GetPoint write SetPoint;

In fact, the class may not have a corresponding field, an array. Recall that the entire processing of calls to the internal structures of the class can be masked.

For a vector property, you must describe not only the type of the array elements, but also the name and type of the index. The read and write keywords must be followed by method names in this case—the use of array fields here is not allowed. A method that reads the value of a vector property should be described as a function that returns a value of the same type as the property's elements and has a single parameter of the same type and with the same name as the property index:

function GetPoint(Index:Integer):TPoint;

Similarly, a method that places values in such a property must have an index as the first parameter, and a variable of the desired type (which can be passed both by reference and by value):

procedure SetPoint(Index:Integer; NewPoint:TPoint);

Vector properties have another important feature. Some classes in Delphi (TLists, TStrings) are "built" around a basic vector property (see Chapter 7). The main method of such a class gives access to some array, and all other methods are as if auxiliary. Especially to facilitate the work in this case, the vector property can be described with the default keyword:

TMyObject = class;
property Strings[Index: Integer]: string read Get write Put; default;

If an object has such a property, then you can not mention it, but put the index in square brackets immediately after the name of the object:

var AMyObject: TMyObject;
AMyObject.Strings[1]: = 'First'; {первый способ}
AMyObject[2]: = 'Second'; (второй способ}

Be careful when using the default reserved word – as we have seen, for regular and vector properties it is used in different cases and with different syntax.

The role of properties in Delphi is eloquently evidenced by the following fact: all the standard classes available to the programmer 100% of the fields are not available and are replaced by properties based on them. We recommend that you follow the same rule when developing your own classes.

An attentive reader noticed that when explaining the terms "field" and "property", we used the concept of a method, and probably understood its general meaning. So, a method is a function or procedure declared in a class that is used to work with the fields and properties of a class. According to the principles of the PLO (see sect. "Encapsulation" later in this chapter), you can access the properties of a class only through its methods.

Methods differ from conventional procedures and functions in that they are given a pointer to the object that called them. Therefore, the data of the object that called the method will be processed. On some features of the use of methods, we will dwell below.