Name

Published Directive

Syntax

type Class declaration
published
   Field declarations...
   Method and property declarations...
end;

Description

The published directive introduces the published section of a class declaration. Just like public declarations, published fields, methods, and properties are accessible from any other subroutines or method in the same unit or in any unit that uses the declaring unit (provided the class declaration is in the unit’s interface section).

Tips and Tricks

  • The difference between public and published is that published declarations tell the compiler to store runtime type information about the published fields, methods, and properties.

  • The compiler treats a published section like public unless RTTI is enabled with the $M or $TypeInfo compiler directives, or if the class inherits from a class with RTTI enabled. TPersistent in Delphi’s Classes unit enables published RTTI, so all persistent classes (including all components, controls, and forms) have RTTI. The initial, unnamed section is published for classes with RTTI.

  • Delphi’s IDE relies on the initial, unnamed section of a form class for storing its fields and methods. Although the compiler will let you move these declarations to any published section of the class, the IDE requires the declarations be in the unnamed section. Because forms and data modules have TPersistent as an ancestor, the initial, unnamed section is published.

  • Published fields must be of class type, and that class must also have RTTI enabled. ...

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.