About Fields

A field is like a variable that belongs to an object. Fields can be of any type, including class types. (That is, fields can hold object references.) Fields are usually private.

To define a field member of a class, simply declare the field as you would a variable. All field declarations must occur before any property or method declarations. For example, the following declaration creates a class called TNumber whose only member, other than the methods is inherits from TObject, is an integer field called Int.

type TNumber = class Int: Integer; end;

Fields are statically bound; that is, references to them are fixed at compile time. To see what this means, consider the following code.


TAncestor = class

Value: Integer; end;

TDescendant = class(TAncestor)

Value: string; // hides the inherited Value field end;

MyObject: TAncestor; begin

MyObject := TDescendant.Create; MyObject.Value := 'Hello!' // error

(MyObject as TDescendant).Value := 'Hello!' // works! end;

Although MyObject holds an instance of TDescendant, it is declared as TAncestor. The compiler therefore interprets MyObject.Value as referring to the (integer) field declared in TAncestor. Both fields, however, exist in the TDescendant object; the inherited Value is hidden by the new one, and can be accessed through a typecast.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment