1. Computing
Understanding the Delphi unit source
Page 1: Delphi Unit source code
More of this Feature
Page 2: Keywords
Join the Discussion
"Post your views and comments to this chapter of the free Delphi Programming Course"
Discuss!
Related Resources
A Beginner's Guide to Delphi Programming.TOC
Project Unit
Unit Sections
TForm: Birth/Life/Death

Welcome to the fifth chapter of the FREE online programming course:
A Beginner's Guide to Delphi Programming.
Take a closer look at exactly what each keyword means by examining each line of the Delphi form unit source code. Interface, implementation, uses and other keywords explained in easy language!

Understanding the unit source
In the previous chapter you have created your second simple Delphi application without going into details about the Delphi Pascal keywords that appear in each Delphi unit. This time, our task is to take a look at exactly what each keyword means by examining each line of the forms unit source code.

As you know already by now, forms are visible building blocks of all (well, at least 99%) Delphi projects. Each form in a Delphi project 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.

The best way to describe the unit code is to take a look at the source. For the moment reefer to the example in the last chapter, especially the unit source. After we have placed a Label, an Edit box and a Button, and added an OnClick event handling procedure for the button, the source code looked like:

01: unit Unit1;

02: interface

03: uses
03:  Windows, Messages, SysUtils, Variants, Classes,
03:  Graphics, Controls, Forms, Dialogs, StdCtrls;

04: type
05:   TForm1 = class(TForm)
06:     Edit1: TEdit;
07:     Button1: TButton;
08:     Label1: TLabel;
09:     procedure Button1Click(Sender: TObject);
10:   private
11:     { Private declarations }
12:   public
13:     { Public declarations }
14:   end;

15: var
16:   Form1: TForm1;

17: implementation
18: {$R *.dfm}

19: procedure TForm1.Button1Click(Sender: TObject);
20: var s: string;
21: begin
22:  s := 'Hello ' + Edit1.Text + ' Delphi welcomes you!';
23:  ShowMessage(s);
24: end;

25: end.

We'll now explore and try to figure what each line stands for. First, a note: Delphi units follow a predefined format. For example, the UNIT keyword must be the first line in the source, followed by INTERFACE... This strict format is "predefined" by Delphi, meaning that when you add an empty form to a project, its associated unit source is already created with special keywords and type declaration. When you add a component on a form and write an event handler for it, Delphi will place the corresponding code at the exact location in the unit file.

Note another thing: in the above code, black lines appear in the forms unit source the first time you add a form to a project. Lines colored in green were added by Delphi when you have placed those three components on a form. Lines in red were added by you in the previous chapter. Whenever you create a new form, Delphi creates the corresponding unit file with the skeleton code marked black.

The rest of the article will discuss parts of the unit source. Several new words like class, object and similar will be mentioned, do not get frightened if you do not understand what they mean, let's just say that such words are a part of Delphi object oriented programming, we will discuss them in the following chapters more clearly.

Next page > Unit keywords explained > Page 1, 2

A Beginner's Guide to Delphi Programming: Next Chapter >>
>> An introduction to Delphi Pascal

©2013 About.com. All rights reserved.