Delphi : Unit and its anatomy

Search for a command to run...

No comments yet. Be the first to comment.
ASP.NET Core Configuration Guide Overview ASP.NET Core uses a layered configuration system where later sources override earlier ones for the same key. Configuration Priority Order Priority Source
Legacy Delphi applications often struggle with outdated UI/UX, performance issues, and monolithic architectures that make refactoring difficult. However, a full rewrite is not always feasible due to time, cost, and the risk of disrupting critical bus...
Entity Framework Core (EF Core) is a powerful and flexible object-relational mapping (ORM) framework for .NET applications, enabling developers to work with relational databases using strongly-typed .NET objects. It simplifies data access by abstract...

In this article we are going to cover some advance scenarios where Options pattern is very useful. We have covered the basic understanding of Options pattern into Options pattern in ASP.NET Core: Introduction article. Please go through it if not fami...

In this article we will learn about Options pattern in ASP.NET Core for dealing with application configurations. The Options pattern uses the C# classes to access and manage the configurations thus providing more flexibility and type safe approach to...

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 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.
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.
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.
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.
This uses section should contains the units which will be needed inside functions and procedures of implementation section.
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.
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!