next up previous contents index search.gif
Next: 4.3 Constructors and destructors Up: 4. Objects Previous: 4.1 Declaration

4.2 Fields

Object Fields are like record fields. They are accessed in the same way as you would access a record field : by using a qualified identifier. Given the following declaration:

Type TAnObject = Object
       AField : Longint;
       Procedure AMethod;
       end;
Var AnObject : TAnObject;
then the following would be a valid assignment:

  AnObject.AField := 0;
Inside methods, fields can be accessed using the short identifier:

Procedure TAnObject.AMethod;
begin
  ...
  AField := 0;
  ...
end;
Or, one can use the self identifier. The self identifier refers to the current instance of the object:

Procedure TAnObject.AMethod;
begin
  ...
  Self.AField := 0;
  ...
end;
You cannot access fields that are in a private section of an object from outside the objects' methods. If you do, the compiler will complain about an unknown identifier. It is also possible to use the with statement with an object instance:

With AnObject do
  begin
  Afield := 12;
  AMethod;
  end;
In this example, between the begin and end, it is as if AnObject was prepended to the Afield and Amethod identifiers. More about this in section With

root
1999-06-10