Name

Object Keyword

Syntax

type Name = Subroutine header of object;

type Name = object
  Declarations...
end;

type Name = object(Base class)
  Declarations...
end;

Description

The object keyword has two distinct and unrelated uses: to declare method types and to declare old-style classes.

  • Delphi Pascal lets you declare a procedure or function type as a plain subroutine or as a method. When you declare a plain procedural type, the type is a plain pointer type. You can assign pointers to subroutines whose signatures match the type, and the pointer value is an ordinary code pointer.

    Using the of object syntax, the procedural type becomes a method type. Methods have two parts: a code pointer and a data pointer. When you take a method’s address, you are capturing the code pointer for the method’s code (which is similar to a plain procedural pointer) and a data pointer (which is the object reference or class reference for a class method).

  • Object-type declarations are obsolete and have been replaced by class declarations. The old-style object declarations exist primarily for backward compatibility with Turbo Pascal. New Delphi programs should use class declarations instead.

Tips and Tricks

  • Properties of method type are called events.

  • The TMethod type is a record that holds a generic method pointer. You can cast TMethod to a particular type, such as TNotifyEvent to call the method.

Examples

type
  TNotifyEvent = procedure(Sender: TObject) of object; TSimpleComponent = class(TComponent) private fOnChange: ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.