Property Access

Properties of a Class, Record or Interface can be accessed simply by specifying their name.

For properties of the current type (i.e. the class or record that the current code is also a part of), simply the name of the property on its own suffices to access it. To access properties of a different type (or a different instance of the same type), a Member Access Expression is used, appending a . or : to the expression that represents the instance, followed by the property name:

type
  Foo = public class
  private
    property Value: String;
    
  public
  
    method CopyFrom(aBar: Foo);
    begin
      Value := aBar.Value;
    end;

  end; 

In the above example, Value can be accessed directly, to get its value in the current instance of the class. A Member Access Expression is used to access the same property on aBar – a different object.

Using self to Avoid Ambiguity

The self expression can be used to explicitly access a property on the current instance, in cases where the name of the field is hidden by a different identifier in scope:

method UpdateValue(Value: String);
begin
  self.Value := Value;
end;

Although in general it is advised to avoid such issues by having a consistent naming schema, such as an a prefix for parameter names:

method UpdateValue(aValue: String);
begin
  fValue := aValue;
end;

Writing to Properties

Unless a property is readonly (either by omitting a setter, or having the [readonly](../Members/Properties#other-modifiers modifier), property access expressions can also be assigned to, to update the value of the property (as already seen in the code snippet above).

Value := 'Hello';

Accessing Indexer Properties

Indexer Properties are a special kind of property that do not represent a single value, but have a range of values controlled by one or more parameters (or "indexes").

Indexer properties must be accessed by appending an Indexer Expression, consisting of square brackets and one or more parameters, to the name. This applies both to reading and (where permitted) writing the property.

If an indexer property is marked as default, it can also be accessed by using the indexer expression directly on the type instance (or on self).

var i := lList.Items[5];
lList.Items[5] := "New Value";

Note that a property access expression to an indexer property is not valid without the appended indexer access expression. e.g.:

var items := lList.Items; // 🛑 compiler error!

See Also