Delphi : Unit and its anatomy

Delphi : Unit and its anatomy

Units are the basic of Delphi programming language. A Delphi unit is a collection of types (classes and data type) constants , variables , functions and procedure. Each Delphi unit has its own pas file. (.pas). When we create a new Delphi project , IDE creates all the necessary files for that project , this includes unit files as well.

Structure of a Delphi unit

Structure of a blank unit is as below :

unit Demo;

interface

uses  // Other units which needs to be referenced

implementation

uses  // Other units which needs to be referenced

initialization // Optional

finalization // Optional
end.

Interface section

This section starts with reserved keyword interface and continue till beginning of word implementation . This section could have variables , constants , types such as class and other types , functions and procedures. Note that functions and procedures could only have their signature inside interface section , not their definition.

Declaration done inside interface section can be called as public as other unit's code can access these entities.

uses - interface section

This section lists the name of units which will be used by this unit. For example when we create a new VCL Forms Application (File -> New -> VCL Forms Application ) , Delphi adds few basic units such as Winapi.Windows,System.SysUtils,Vcl.Forms etc.

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

As a best practice , we should only list units in this uses clause which will be needed inside interface section. However Delphi does not restrict to list units in this section which will be used inside implementation section.

implementation section

Similar to interface section , implementation section could also have variables, constants ,types (classes and other types ) , procedures and functions. In additions to this it also has definition of the routines defined in interface section.

Declaration done inside interface section can be called as private as other unit's code can not access these entities.

uses - implementation section

This uses section should contains the units which will be needed inside functions and procedures of implementation section.

initialization

This section is optional and not part of the default unit structure generated by Delphi IDE. This section is used to initialize any data which will be used by the unit.

initialization section of the units used in uses is executed in the same order as they are listed.

finalization

This section is optional and can only be used if unit has initialization section. This section can be used to clean up the resources when the main program is terminated. In short, you could free the resources which were initialized in initialization section.

Happy coding!

Did you find this article valuable?

Support Techies Diary by becoming a sponsor. Any amount is appreciated!