Within the implementation of a method, the identifier Self references the object in which the method is called. For example, here is the implementation of TCollection's Add method in the Classes unit.

function TCollection.Add: TCollectionItem; begin

Result := FItemClass.Create(Self);


The Add method calls the Create method in the class referenced by the FItemClass field, which is always a TCollectionItem descendant. TCollectionItem.Create takes a single parameter of type TCollection, so Add passes it the TCollection instance object where Add is called. This is illustrated in the following code.

var MyCollection: TCollection;

MyCollection.Add // MyCollection is passed to the TCollectionItem. Create method

Self is useful for a variety of reasons. For example, a member identifier declared in a class type might be redeclared in the block of one of the class' methods. In this case, you can access the original member identifier as Self.Identifier.

For information about Self in class methods, see Class methods.

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