Delphi Program Structure

Source: Internet
Author: User

Reproduced to: http://www.cnblogs.com/hackpig/archive/2010/02/15/1668513.html

Overview:
The structure of Object Pascal is quite special, which is quite different from that of C. However, it adheres to the consistent structured tradition of Pascal and is easy to understand.

1. Program unit
An object PASCAL program is composed of a special unit and several optional units. That is to say, the simplest Object Pascal program can be composed of only one special unit, this special unit is the Program unit. In Delphi, the program unit is the Delphi project file. In terms of functions, the program unit is a bit like the main program in C language. The program is simple, and it also has to have a main function. The Program unit is such a main program. Of course, if a program only has a program unit, it does not make much practical sense (except for programs that complete special tasks). In fact, Delphi programs have at least one subroutine, that is, the subprogram unit to be introduced later.

A typical program unit is listed below:

Program project1;
Uses
Forms,
Unit1 in 'unit1. pa ';

{$ R *. Res}

Begin
Application. initialize;
Application. createform (tform1, form1 );
Application. Run;
End;

The Program unit consists of the following parts:

Program header, which consists of the reserved word Program followed by a Program name. In the preceding example, project 1 is used. The program header can also contain parameters,
However, it is rarely used.

The reference part consists of the reserved word Uses and the name of the unit to be referenced. There are two in the preceding example: Forms unit and Unit1 unit,
Unit Unit1 is also specified in the Unit1.pas file. The simplest program can have no USES part.

In the preceding example, only one Compilation instruction {$ R *. RES} is available.

The execution part is also called the initialization part, which is a piece of code enclosed by the reserved word Begin and End.

Note: Except for some special needs, if you check whether the second instance is running, you rarely need to manually modify the Program unit because Delphi can automatically establish and maintain this unit, for example, when you start a new job type, Delphi automatically creates a Program unit and a subroutine unit. When you add a new Form or Unit to the Project, or use Project | Option... The sub-command modifies the program name, and the program unit is automatically updated.

Ii. Unit)
The subprogram unit is called only to distinguish it from the Program unit. Most of the time, we almost do not need to contact the Program unit, so we will refer to the unit as the subprogram unit.
A unit of Object pascal is an independent Pascal source file with the extension. Pas. The unit structure consists of the following parts:

Header
Interface Section
Implementation
Initialization
Termination part
End;

1: unit header
The Unit header is similar to the Program Unit header. It consists of a reserved word Unit and a Unit name, for example:

Unit Unit1;

Note: The unit header is also a complete statement. Therefore, the unit name should be followed by a semicolon. In addition, the unit name must comply with the Object Pascal rules on identifiers, the unit name must be unique in the same project.

When you save the file, the file name you use will be automatically reflected in this section. It will also be added to the USES section of the Program unit. Therefore, if a file is missing in your project, you can check whether the path and name of the file have been changed. Files in the DELPHI project do not need to be stored in the same directory (but I suggest you do this for ease of management ).

2: Interface
The Interface part is called the Interface part. It is used to declare the referenced, constant, type, variable, process, and function. The Implementa-tion part of the unit (implementation part) you can also declare the referenced unit, constant, type, variable, so what is the difference between the two?

The reference unit, constant, type, variable, process, and function declared in the Interface part are public to the whole program, that is, for all units that reference the unit, these declarations are both visible and accessible. For example, the units that reference this unit can call the processes and functions declared in the Interface. The Interface part is equivalent to the Public part of the class type.

The Interface part starts from the reserved word Interface and ends before the reserved word Implementation.

In the Interface section, procedures and functions only need to write their headers. The specific definition is given in the Implementation (Implementation section) below.

The Interface itself can be composed of several optional parts, namely the unit reference part (Uses), constant declaration part, type declaration part, variable declaration part, process and function declaration part.

The Unit reference section (uses) is used to list the standard unit and other units to be referenced by the Unit. The concept of unit reference is somewhat similar to the INCLUDE in C language. It is used to introduce external declared constants, types, variables, procedures, or functions into this unit, standard units refer to the predefined units of Object Pascal, such as Windows, SysUtils, and Forms. You can also add other non-standard units to Uses, in this way, the Unit can reference constants, types, variable processes, and functions in these units. When you add a control to a form, the unit of the control is automatically added to this part. If you do not see it, you can see it on the storage disk.

The following is an example:

Unit Unit1;
Interface
Uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Diglogs;
Type
Tform1 = Class (Tform)
Private
Public
End;
Var
Form1: Tform1;

Implementation

{$ R *. DFM}
End.

This is a typical unit automatically generated by Delphi. It only gives the structure of this form. In the uses section, units such as Windows, messages, sysutils, classes, and graphics are introduced, so that you can directly call the routines in these units without adding the unit reference name. For example:
Messagebeep (0 );

This is not required:
Windows. messagebeep (0 );

Although this routine is declared in a Windows unit.
Note: If the Interface part contains the uses part, the uses reserved word must be followed by the reserved word interface, although it can be different from the same row.

In the above example, all referenced units are standard units, and some of them are used in almost all units. Therefore, Delphi automatically adds these units to the uses part of your unit, however, some units are not commonly used. If you want to reference them in your program, you must manually add them to the uses section. For example, if you need to use the sndplaysound function in a program to play a WAV file, and this function is declared in the mmsystem unit, you need to add mmsystem to the uses part, before adding a semicolon, separate them with commas.

The order of adding to uses units is generally irrelevant, but some units such as sharemem must be placed in the first one.
The last point is that if the Interface part has these parts at the same time, it must be arranged in the order of unit reference, constant declaration, type declaration, variable declaration, process, and function.

3: Implementation
The implementation part is divided into two parts, one of which is the declaration part, including unit reference, constant, type, variable, process and function declaration. This is similar to the interface part, the Unit references, constants, types, variables, procedures, and functions declared in implementation are only public and visible to the current unit. Other units cannot access them even if they are referenced. Another difference is that the process and function declared in implementation do not need to follow the rules defined first, but simply write the process or function definition directly.

Another major part of implementation is the process declared in interface and the definition of functions. The program example is as follows:

Unit unit1;
Interface
Uses Windows, messages, sysutils, classes, graphics, controls, forms, diglogs;
Type
Tform1 = Class (tform)
Procedure formcreate (Sender: tobject );
Private
Public
End;
VaR
Form1: tform1;

Implementation

{$ R *. DFM}
Procedure beep;
Begin
Messagebeep (0 );
End;
Procedure tform1.formcreate (Sender: tobject );
Begin
Beep;
End;
End.

4: initialization
The initialization part can be included in the unit. This part is also called the initialization part, which is used to initialize the unit. For example, assigning initial values to variables and allocating resources is rarely used.

The initialization part consists of the reserved word initialization and some statements. The statement can be a single statement or a compound statement. If it is a compound statement, it should be enclosed by begin and end.

If multiple units contain the initialization part, the execution sequence of these units is consistent with that of the uses part of the program unit.

5: finalization
If the unit has the initialization part, the Unit can have finalization. the syntax of the termination part is similar to that of the initialization part. It consists of the reserved word fnialization and some statements. The statement can be a single statement, it can also be a composite statement. If it is a composite statement, it should be enclosed by begin and end.

If multiple units contain the finalization part, the execution sequence is the opposite to that of initialization.

Note: The finalization part of the Code should take into account such a situation, that is, the initialization part of the unit may be accidentally terminated during execution, that is, such a situation may occur, that is, if the value of some pointers is nil, the corresponding termination should avoid reference to these pointers.
 

This chapter describes the structure of the object Pascal program. In the next chapter, I will introduce the object Pascal Language's "class types and class references", which will be the basis of Object Pascal's object-oriented programming.

★Note: The content described in the next chapter will involve the advanced part of the Object Pascal language. It is recommended that you refer to the previous section
Review the content of Chapter 6. Otherwise, it may cause difficulties in understanding the content of the subsequent chapter.

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.