Unit FileIs a Pascal source file with the extension:. Pas.
There are three types of unit files:
- Form/data module and framework unit file(Form/data module and frame units), which is generally automatically generated by Delphi.
- Component unit file(Component units), which is generated by you or Delphi.
- Common Unit files(General-purpose units), which has been created by you.
Let's take a look at the basic Pascal unit file structure?
Step 1. Select 【File | new | Unit]. Delphi will create a new unit and display it in code editor.
Step 2. Let's take a look at the simplest Pascal Unit, which includes four keywords.Unit,Interface,ImplementationAndEnd;
For more information about the units, see the notes:
Unit unit1; interfaceuses {list of units goes here} windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; {interface section goes here} type {type keyword is used to declare a New Type} tmyarray = array [0 .. 19] of byte; {declare tmyarray to replace array [0 .. 19] of byte} const {const keyword used to declare a constant} appcation = 'Hello world'; {appcation because it is declared in the interface segment, it can be used anywhere in the unit to declare variables with the} var {var keyword. It can also be divided into interface and implementation segments} X: integer; myarry: tmyarray; {myarray is the new tmyarray type} procedure dosomething; {declares a dosomething process} declare {list of units goes here} sysutils, variants; var objlist: tobjectlist; const {basex, basey because it is declared in implementation segment, it can only be used in the unit} basex = 20; basey = 200; {dosomething process implementing interface segment declaration} procedure dosomething; begin {code for dosomething goes here .} end; // C ++-style comments, which can only be used for single line comments (* comments of the same type cannot be nested *) {comments} {implementation section goes here} initialization {initialization section goes here} objlist: = tobjectlist. create; finalization {finalization section goes here} freeandnil (objlist); end.
Uses unit reference
An external unit list referenced by a unit. Each unit must be separated by a comma. The last unit must have a plus sign. A semicolon indicates the end of the uses list.
Interface segment
The output identifier of the unit, which can be accessed by other units. The interface segment starts with interface and ends with implementation.
Implementation Execution Section
The execution segment starts with implementation and ends with the next keyword. The next keyword is usually the end keyword of the unit. However, in an initialized unit, the next key is the initialization keyword.
The preceding three parts are mandatory for unit units. The following two keywords are optional.
Initialization unit initialization and finalizaiton unit termination
Code used to execute startup and cleanup. Any code in Initialization will be executed when the unit is loaded into the memory. Any code in the end segment will be executed before the unit is cleaned up from the memory.
There can be only one initialization segment, but not the end segment.