DelphiBasics
Uses
Keyword
Declares a list of Units to be imported
1   Uses Unit1 {Unit2, ...};
2   Uses Unit1 in FileName1 {Unit2, ...};
Description
The Uses keyword defines a list of one or more units that are used by the current unit, program or library.
 
Each unit is effectively imported - all public or published routines and data in each unit is made available.
 
For example, including SysUtils makes available a lot of data manipulation routines, such as IntToStr.
 
The order of the units in the list is important. Those later in the list take precedence over the earlier ones. In the rare situation where the same routine or data is defined in two or more units, you can force Delphi to use the desired unit by prefixing that unit name to the routine or data name.
 
For Units, the Uses may be defined in the Interface and Implementation sections. It is cleaner and may save recompilations if you put units used in the implementation section if they are only used there.
 
Version 1 is the standard form - it lets Delphi find the units.
 
Version 2 defines the FileName, including path, where the unit resides. This is only applicable to programs and libraries.
Notes
System is included by default - there is no need to explictly include it.
Related commands
Implementation Starts the implementation (code) section of a Unit
Interface Used for Unit external definitions, and as a Class skeleton
Unit Defines the start of a unit file - a Delphi module
 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;

interface         // Defines the external view of this unit

Uses
  Forms;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

Implementation   // Implements the Interface of this unit
{$R *.dfm}       // Include form definitions

Uses             // Private units
  Dialogs, SysUtils;

// A private routine - not predefined in the Interface section
procedure SayNumber(number : Integer);
begin
  // Say a number as a string
  // Note that we are very specific about the IntToStr routine :
  // we are making it unambiguous that it is the SysUtils
  // version that we are using.
  ShowMessage('Number = '+SysUtils.IntToStr(number));
end;

// A routine pre-defined in the Interface section
procedure TForm1.FormCreate(Sender: TObject);
begin
  // Display a number
  SayNumber(123);
end;

end.
   Number = 123
 
Delphi Programming Neil Moffatt 2002 - 2016. All rights reserved.  |  Home Page