Field Access

Fields of a Class or Record can be accessed simply by specifying their name.

For fields of the current type (i.e. the class or record that the current code is also a part of), simply the name of the field on its own suffices to access it. To access fields 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 field name:

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

  end; 

In the above example, fValue 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 field on aBar – a different object.

Note that non-private fields are discouraged, so accessing fields (opposed to, say, Properties) on other instances is a rare (but not entirely uncommon) situation. In the example above, you notice that a class can access private fields of other instances of the same class

Using self to Avoid Ambiguity

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

var Value: String;
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 f prefix for fields, and a prefix for parameter names:

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

Writing to Fields

Unless a field is marked as readonly, field access expressions are assignable, allowing code to to update the value of the field (as already seen in the code snippet above).

fValue := 'Hello';

See Also