1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/od/beginners/l/aa051199.htm

was emailed to:

Thanks for sharing About.com with others!

ANATOMY OF A DELPHI UNIT
Delphi For Beginners:
Interface, Implementation, Initialization, Finalization, Uses and other "funny" words!

If you plan on being a good Delphi programmer than words like interface, implementation, 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 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.

   Anatomy
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.

//**blank form unit
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, 
Graphics, Controls, Forms, Dialogs;
type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.DFM}
end. //this ends the unit

//**blank code unit
unit Unit2;
interface
{interface code goes here}
implementation
{implementation code goes here}
end. //this ends the unit

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. I'll be dealing with variable scope; procedures and functions in some future articles.

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

unit UnitName:
....
initialization
 {initialization code goes here}
finalization
 {finalization code goes here}
end.

These two sections are optional; they are not automatically generated when we create a unit. If we want to initialize any data the unit uses, we 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.

   Related articles

  • More Delphi for Beginners
  • Working with units - review of two basic building blocks of a Delphi program: forms and units.
  • ©2014 About.com. All rights reserved.