Here we are going to have a look at a basic unit.
Units are modules which are parts of your program, an example is that you can have 20 units in one program, or even more if you need them.
Another example is in a quiz program a unit can hold all the questions
and answers, another unit can be for comments made in the program, in other
words you can break down your program into logical units.
If your program is large these units make it easier for you to remember
where you have put each part of the program, as the example above shows,
you can easily find all the questions for your quiz program because they
are all in the
questions unit.
When you start a new project in Delphi you will find that Delphi will automatically create a program module and unit that defines the main form, Form1.
If you decide you want to add a new form, maybe an AboutBox then Delphi will create a new unit for that AboutBox and any other new forms you add to your program.
Knowing what a unit is and how its put together will help you become a better Delphi programmer.
A Delphi unit has the file extension .pas, so unit1 will have a pascal
file called unit1.pas.
The program files have the extension of .dfm.
Here is a basic 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.
==============================================================
Now lets break it down and have a look at the parts to a unit and what they mean.
First we have the unit keyword
------------
unit Unit1;
------------
Next to the unit keyword is the name of the unit, (in this case the
name of the unit is Unit1.pas) and next to the unit keyword you see the
word Unit1 which represents the Unit1.pas file.
The name next to the word "unit" must be the same as the name of the
unit otherwise an error message will show.
So if your unit is called quest1.pas then you must have
------------
unit Quest1;
------------
at the top of that unit.
Next we have the interface section.
The interface section starts at the interface word in the unit and
finishes at the implementation statement, so everything between interface
and implementation is part of the interface section.
Everything that in the interface section is visible to any units that
use this unit.
The Uses clause, this is where units that your program is dependent
on are listed.
You can see the standard ones here:
-------------
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs;
-------------
When you add a component to the form (Form1) you can then see Delphi
has added the unit for that component here in the Uses clause.
It is not often that you will want to add your own units names here
because you can add them after the implementation statement in your
own Uses clause, you will see many examples of this when you view the source
code of many
Delphi projects.
Type Clause:
This is where the Type declaration of the form begins.
When you start a new project, Delphi declares a new class type for
the form, this new class type is TForm, which is derived from type TForm
which is also a class.
-------------
type
TForm1 = class(TForm)
-------------
If you add a component to your form (Form1) then you will notice that
the source code in the unit changes like this (we will add a Memo):
-------------
type
TForm1 = class(TForm)
Memo1: TMemo;
-------------
Should you decide to change the name of the memo to FirstMemo using
the Object Inspector, then you will see this change in the Delphi Code
Editor:
-------------
type
TForm1 = class(TForm)
FirstMemo: TMemo;
-------------
Notice how Delphi makes these changes for you.
You can use the Object Inspector to make changes such as changing the
name of a component.
The Implementation Section:
This is everything between the implementation statement and either
the finalization statement or the end of the pascal unit file.
The finalization section is optional and the code in the finalization
section is executed upon program shutdown.