Unit file pas constructed by Delphi project

Source: Internet
Author: User

Unit FileIs a Pascal source file with the extension:. Pas.

There are three types of unit files:

  1. Form/data module and framework unit file(Form/data module and frame units), which is generally automatically generated by Delphi.
  2. Component unit file(Component units), which is generated by you or Delphi.
  3. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.