DelphiBasics
Unit
Keyword
Defines the start of a unit file - a Delphi module
  Unit Name;
    Interface
      ...
    Implementation
      ...
  {Initialization
      ...}
  {Finalization
      ...}
  end.  // Note terminating dot
Description
The Unit keyword is fundamental to Delphi, defining the start of a module.
 
Each Delphi module is stored in a Name.pas file, where Name must match the Unit Name.
 
Each unit comprises a set of interface definitions. Do not confuse these with class interfaces - the unit interface is the external interface to the module. It defines to any user of the unit what it contains. These interface definitions define private, protected, public and Published data, types and routine declarations.
 
Any routines defined in the interface must be implemented in the Implementation section. All function and procedure definitions must have exactly redefined arguments - see procedure and function information.
 
Optionally, you may have Initialization and Finalization sections. These contain statements executed when the unit is loaded and when it terminates, respectively.
 
If you only define an Initialization section, you may replace this keyword with Begin.
Related commands
Function Defines a subroutine that returns a value
Implementation Starts the implementation (code) section of a Unit
Interface Used for Unit external definitions, and as a Class skeleton
Private Starts the section of private data and methods in a class
Procedure Defines a subroutine that does not return a value
Property Defines controlled access to class fields
Protected Starts a section of class private data accesible to sub-classes
Public Starts an externally accessible section of a class
Published Starts a published externally accessible section of a class
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 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;

// External interface to this unit module
interface

uses
  Forms, Dialogs;

type
  // Define the form class used by this unit
  TForm1 = class(TForm)
    // Define a procedure that must be implemented in the
    // implementation section below
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  // Define a form - can be referenced in the Implementation section
  Form1: TForm1;

// Implements the interface declarations above
implementation
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Set and show the form title
  Form1.Caption := 'Test program';
  ShowMessage('Running = '+Form1.Caption);
end;

initialization
  // Tell the user we are starting this unit
  ShowMessage('Initialising');

end.
Show full unit code
   Initialising
   Running Test program
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page