Delphi Language Overview

Source: Internet
Author: User

Delphi is a high-level, compiled, strongly typed language, supports structured and object-oriented design. Based on Object Pascal, it benefits include Easy-to-read code, quick compilation, and the use of multiple unit files for Modular programming. Delphi has special features, the RAD Studio component framework and environment. For the most part, descriptions and examples in this language guide assume that is using Embarcadero development tool S.

Most developers using Embarcadero software development tools write and compile their code in the integrated development EN Vironment (IDE). Embarcadero Development Tools handle many details of setting up projects and source files, such as maintenance of Dependen CY information among units. The product also places constraints on program organization that is not, strictly speaking, part of the Object Pascal LAN guage specification. For example, Embarcadero development tools enforce certain File-and program-naming conventions so can avoid if you Write your programs outside of the IDE and compile them from the command prompt.

This language guide generally assumes the is working in the IDE and that is building applications that use the Visual Component Library (VCL). Occasionally, however, delphi-specific rules is distinguished from the rules that apply to all Object Pascal programming.

This section covers the following topics:

    • Program Organization. Covers the basic language features that allow you to partition your application into units and namespaces.
    • Example Programs. Small examples of both console and GUI applications is shown, with basic instructions on running the compiler from the CO Mmand-line.
Program Organization

Delphi programs is usually divided into Source-code modules called units. Most programs begin with a program heading, which specifies a name for the program. The program heading are followed by an optional uses clause, then a block of declarations and statements. The uses clause lists units that is linked into the program; These units, which can is shared by different programs, often has uses clauses of their own.

The uses clause provides the compiler with information about dependencies among modules. Because This information are stored in the modules themselves, most Delphi language programs does not require makefiles, head Er files, or preprocessor "include" directives.

Delphi Source Files

The compiler expects to find Delphi source code in Files of three kinds:

    • Unit source files (which end with the. pas extension)
    • Project files (which end with the. DPR extension)
    • package source files (which end with the .dpk extension)

The Unit source files typically contain most of the code in an application. Each application have a single project file and several unit files; The project file, which corresponds to the program file in traditional Pascal, organizes the unit files into an A Pplication. Embarcadero Development tools automatically maintain a project file for each application.

If you is compiling a program from the command line, you can put all your source code into unit (. pas) files. If you use the IDE to build your application, it'll produce a project (. DPR) file.

Package source files is similar to project files, but they is used to construct special dynamically linkable libraries C alled packages.

Other Files used to Build applications

In addition to Source-code modules, Embarcadero products use several non-pascal files to build applications. These files is maintained automatically by the IDE, and include

    • VCL form files (which with a. dfm extension on Win32)
    • Resource files (which end with. Res)
    • Project options files (which end with. DOF)

A VCL Form file contains the description of the form and the components it owns. Each of the form file represents a single form, the which usually corresponds to a window or dialog the box in an application. The IDE allows you to view and edit form files as text, and to save form files as either text (a format very suitable for Version control) or binary. Although the default behavior is to save form files as text, they was usually not edited manually; It's more common to use Embarcadero's visual design tools for this purpose. Each project have at least one form, and each form have an associated unit (. pas) file, by default, with the same name a s the form file.

In addition to VCL form files, each project uses a resource (. res) file to hold the application ' s icon and other resources such as strings. By default, this file has the same name as the project (. DPR) file.

A Project Options (. dof) file contains compiler and linker settings, search path information, version information, and so Forth. Each project has a associated project options file with the same name as the project (. DPR) file. Usually, the options in this file is set from Project Options dialog.

Various tools in the IDE store data in files of other types. Desktop settings (. DSK) files contain information about the arrangement of Windows and other configuration options; Desktop settings can be project-specific or environment-wide. These files has no direct effect on compilation.

compiler-generated Files

The first time you build a application or a package, the compiler produces a compiled unit file (. DCU in Win32) for EA CH new unit used in your project; All the. dcu files in your project is then linked to create a single executable or shared package. The first time you build a package, the compiler produces a file for each new unit contained in the package, and then Crea TES both a. DCP and a package file. If you use the GD compiler switch, the linker generates a map file and a. drc file; The. DRC file, which contains string resources, can be compiled into a resource file.

When you build a project, individual units is not recompiled unless their source (. pas) files has changed since the last compilation, their DCU/.DPU files cannot be found, you explicitly tell the compiler to reprocess them, or the interface Of the unit depends on another unit which have been changed. In fact, it isn't necessary for a unit's source file to being present at all, as long as the compiler can find the compiled The unit file and that unit had no dependencies on the other units that had changed.

Example Programs

The examples that follow illustrate basic features of Delphi programming. The examples show simple applications This would not normally is compiled from the IDE; You can compile them from the command line.

A Simple Console application

The program below are a simple console application so can compile and run from the command prompt:

1  Programgreeting;2  3  {$APPTYPE CONSOLE}4  5  var6Mymessage:string;7  8  begin9Mymessage: ='Hello world!';Ten Writeln (mymessage); One  End.
View Code

The first line declares a program called greeting. The {$APPTYPE console} directive tells the compiler that's a CONSOLE application, to be run From the command line. The next line declares a variable called mymessage, which holds a string. (Delphi has genuine string data types.) The program then assigns the string "Hello world!" to the variable mymessage, and sends the contents of Mymessage to the S Tandard output using the writeln procedure. (writeln is defined implicitly in the system unit, which the compiler a Utomatically includes in every application.)

You can type this program to a file called Greeting.pas or GREETING.DPR and compile it by entering:

Dcc32 Greeting

To produce a Win32 executable.

The resulting executable prints the message Hello world!

Aside from it simplicity, this example differs in several important ways from programs, is likely to write with Embarcadero Development tools. First, it is a console application. Embarcadero Development tools is most often used to write applications with graphical interfaces; Hence, you would don't ordinarily call Writeln. Moreover, the entire example program ("Save for Writeln") is in a single file. In a typical GUI application, the program heading the first line of the example would is placed in a separate project file That would is contain any of the actual application logic, and other than a few calls to routines defined in unit files.

A more complicated Example

The next example shows a program, which is divided into the FILES:A project file and a unit file. The project file, which you can save as GREETING.DPR , looks like this:

  1  program   greeting;    3  {   $APPTYPE CONSOLE   "  4   5  uses   6   Unit1;    8    9  printmessage ("  hello world!     10  end . 
View Code

The first line declares a program called greeting, which, once again, is a console application. The uses UNIT1, clause tells the compiler, the program greeting depends on a unit called Unit1. Finally, the program calls the Printmessage procedure, passing to it the string Hello world! The printmessage procedure is defined in Unit1. The source code for UNIT1, which must are saved in a file called Unit1.pas:

1 UnitUnit1;2  3  Interface4  5  procedurePrintmessage (msg:string);6  7  Implementation8  9  procedurePrintmessage (msg:string);Ten  begin One writeln (msg); A  End; -   -  End.
View Code

Unit1Defines a procedure called that takes a single string as an argument and sends the string to the standard PrintMessage output. (in Delphi, routines this does not return a value is called procedures. Routines that return a value is called functions.)

Notice that's PrintMessage declared twice in Unit1 . The first declaration, under the reserved word interface, makes PrintMessage available to other modules (such as greeting ) that use Unit1 . The second declaration, under the reserved word implementation, actually defines PrintMessage .

You can now compile greeting from the command line by entering

dcc32 greeting

To produce a Win32 executable.

There is no need to include as Unit1 a command-line argument. When the compiler processes greeting.dpr , it automatically looks for unit files and the program greeting depends on. The resulting executable does the same thing as our first example:it prints the messageHello world!

A VCL Application

Our next example is a application built using the Visual Component Library (VCL) in the IDE. This program uses automatically generated form and resource files, so you won ' t is able to compile it from the source code Alone. But it illustrates important features of the Delphi Language. In addition to multiple units, the program uses classes and objects.

The program includes a project file and both new unit files. First, the project file:

1  Programgreeting;2  3  uses4 Forms, Unit1, Unit2;5  6  {$R *.res} {This directive links the project ' s resource file.}7  8  begin9     {Calls to global application instance}Ten application.initialize; One Application.createform (TForm1, Form1); A Application.createform (TForm2, Form2); - Application.Run; -  End.
View Code

Once again is called greeting. It uses three units: Forms, which is part of VCL; Unit1, which is associated with the application ' s main form (Form1); and Unit2, which are associated with Anoth Er form (Form2).

The program makes a series of calls to an object named application, which are an instance of the Vcl.Forms.TApplication class defined in the Forms unit. (Every project has an automatically generated Application object.) The these calls invoke a Vcl.Forms.TApplication method named CreateForm. The first call to CreateForm creates Form1, a instance of the TForm1 class defined in Unit1. The second call to CreateForm creates Form2, a instance of the TForm2 class defined in Unit2.

Delphi Language Overview

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.