Anatomy of a Delphi Unit (Delphi for Beginners)

delphi unit screenshot

If you plan on being a good Delphi programmer than words like "interface," "implementation," and "uses" need to have special place in your programming knowledge.

Delphi Projects

When we create a Delphi application, we can start with a blank project, an existing project, or one of Delphi's application or form templates. A project consists of all the files needed to create our target application. 

The dialog box that pops up when we choose View-Project Manager lets us get access to the form and units in our project. 

A project is made up of a single project file (.dpr) that lists all the forms and units in the project. We can look at and even edit the Project file (let's call it a Project Unit) by choosing View - Project Source. Because Delphi maintains the project file, we should not normally need to modify it manually, and in general it is not recommended for inexperienced programmers to do so.

Delphi Units

As we know by now, forms are a visible part of most Delphi projects. Each form in a Delphi project also has an associated unit. The unit contains the source code for any event handlers attached to the events of the form or the components it contains.

Since units store the code for your project, units are the basic of Delphi programming. Generally speaking, unit is a collection of constants, variables, data types, and procedures and functions that can be shared by several applications.

Every time we create a new form (.dfm file), Delphi automatically creates its associated unit (.pas file) let's call it a Form Unit. However, units don't have to be associated with forms. A Code Unit contains code that is called from other units in the project. When you start building libraries of useful routines, you will probably store them in a code unit. To add a new code unit to Delphi application choose File-New ... Unit.


Whenever we create a unit (form or code unit) Delphi adds the following code sections automatically: unit header, interface section, implementation section. There are also two optional sections: initialization and finalization.

As you will see, units have to be in a predefined format so that the compiler can read them and compile the unit's code.

The unit header starts with the reserved word unit, followed by the unit's name. We need to use the unit's name when we refer to the unit in the uses clause of another unit.

Interface Section

This section contains the uses clause that lists the other units (code or form units) that will be used by the unit. In case of form units Delphi automatically adds the standard units such as Windows, Messages, etc. As you add new components to a form, Delphi adds the appropriate names to the uses list. However, Delphi does not add a uses clause to the interface section of code units—we have to do that manually.

In the unit interface section, we can declare global constants, data types, variables, procedures and functions.

Be aware that Delphi builds a form unit for you as you design a form. The form data type, the form variable that creates an instance of the form, and the event handlers are declared in the interface part. 

Because there is no need to synchronize the code in code units with an associated form, Delphi does not maintain the code unit for you.

Interface section ends at the reserved word implementation.

Implementation Section

The implementation section of a unit is the section that contains the actual code for the unit. The implementation can have additional declarations of its own, although these declarations aren't accessible to any other application or unit. Any Delphi objects declared here would be available only to code within the unit (global to unit). An optional uses clause can appear in the implementation part and must immediately follow the implementation keyword.

Initialization and Finalization Sections

These two sections are optional; they are not automatically generated when you create a unit. If you want to initialize any data the unit uses, you can add an initialization code to the initialization section of the unit. When an application uses a unit, the code within the unit's initialization part is called before the any other application code runs. 

If your unit needs to perform any cleanup when the application terminates, such as freeing any resources allocated in the initialization part; you can add a finalization section to your unit. The finalization section comes after the initialization section, but before the final end.

mla apa chicago
Your Citation
Gajic, Zarko. "Anatomy of a Delphi Unit (Delphi for Beginners)." ThoughtCo, Feb. 16, 2021, Gajic, Zarko. (2021, February 16). Anatomy of a Delphi Unit (Delphi for Beginners). Retrieved from Gajic, Zarko. "Anatomy of a Delphi Unit (Delphi for Beginners)." ThoughtCo. (accessed March 29, 2023).