Starts a published externally accessible section of a class
type Class declaration
  Field | Property | Method declaration
The Published directive starts a declaration section of a class definition. In a published section, Fields, Properties and Methods are declared to be accessible to this class, classes descending from it, and code using object instances of the class. And information about these accesses are recorded in the run time information for the class.
In Object Oriented terms, a class object is seen as a black box. The internal operations are not relevant. Public and published fields, properties and methods are the externally visible part of an object - a controlled access to the internals of the class.
Because public and published access provides a linkage of sorts to external code, you should avoid wherever possible making changes to these sections.
Warning : avoid making fields published - it is always better to define a property to access them instead. This provides some decoupling from the internals of the class.

Only one Constructor may be declared as published - overloaded versions must be defined as public.

Published properties cannot return arrays.
Related commands
FunctionDefines a subroutine that returns a value
PrivateStarts the section of private data and methods in a class
ProcedureDefines a subroutine that does not return a value
PropertyDefines controlled access to class fields
ProtectedStarts a section of class private data accesible to sub-classes
PublicStarts an externally accessible section of a class
TypeDefines a new category of variable or process
 Download this web site as a Windows program.

Example code : A simple example
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;


  Forms, Dialogs, Classes, Controls, StdCtrls, SysUtils;

  // Define a base TSquare class :
  TSquare = class
  private          // Only known internally
    squareArea, squareWidth, squareHeight : Integer;
  Published        // Known externally : has run time info also
    property width  : Integer read squareWidth;
    property height : Integer read squareHeight;
    property area   : Integer read squareArea;
    constructor Create(width, height : Integer);

  // Define the form class used by this unit
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;

{$R *.dfm} // Include form definitions

// Create the TSquare object
constructor TSquare.Create(width, height: Integer);
  // Save the width and height
  squareWidth  := width;
  squareHeight := height;

  // And calculate and save the square area
  squareArea := width * height;

// Main line code
procedure TForm1.FormCreate(Sender: TObject);
  mySquare : TSquare;
  // Create a square object
  mySquare := TSquare.Create(30, 40);

  // What are the square attributes ? :
  ShowMessageFmt('Square area = %d * %d = %d',
                 [mySquare.width, mySquare.height, mySquare.area]);

  Square area = 30 * 40 = 1200
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page