_I_Play_Chess
10-19-2003, 08:32 AM
Chapter 5 Page 1 of 2
Understanding the Delphi unit source
Page 1: Delphi Unit source code
----------------------------------------
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 refer 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:
-----------------------------------------
// Code writen in BLACK:
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)
//Here the code is writen in GREEN:
06: Edit1: TEdit;
07: Button1: TButton;
08: Label1: TLabel;
09: procedure Button1Click(Sender: TObject);
//Code writen in Black and BLUE:
10: private
11: ** Private declarations }
12: public
13: ** Public declarations }
14: end;
15: var
16: Form1: TForm1;
17: implementation
18: **$R *.dfm}
// Code writen in GREEN:
19: procedure TForm1.Button1Click(Sender: TObject);
// Code writen in RED:
20: var s: string;
//Code in GREEN:
21: begin
// Code writen in RED:
22: s := 'Hello ' + Edit1.Text + ' Delphi welcomes you!';
23: ShowMessage(s);
//Code in GREEN:
24: end;
//In Black;
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.
Understanding the Delphi unit source
Page 1: Delphi Unit source code
----------------------------------------
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 refer 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:
-----------------------------------------
// Code writen in BLACK:
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)
//Here the code is writen in GREEN:
06: Edit1: TEdit;
07: Button1: TButton;
08: Label1: TLabel;
09: procedure Button1Click(Sender: TObject);
//Code writen in Black and BLUE:
10: private
11: ** Private declarations }
12: public
13: ** Public declarations }
14: end;
15: var
16: Form1: TForm1;
17: implementation
18: **$R *.dfm}
// Code writen in GREEN:
19: procedure TForm1.Button1Click(Sender: TObject);
// Code writen in RED:
20: var s: string;
//Code in GREEN:
21: begin
// Code writen in RED:
22: s := 'Hello ' + Edit1.Text + ' Delphi welcomes you!';
23: ShowMessage(s);
//Code in GREEN:
24: end;
//In Black;
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.