Only understanding the structure of a Delphi unit is unfortunately, not enough for a Delphi beginner to start working on large sized applications. Developers who are new to Delphi often find that they are uncertain about which unit they need to "use" and how to "use" it.
Before we move on to creating your own code units, let's see how Delphi generates and maintains a form unit source.
Examining a form unit and its uses clause
Every time you start Delphi (or create a new project), the Delphi IDE displays a new form for you to customize. In the Code editor, the automatically generated form unit declares a new class type for the form and includes the code that creates the new form instance.
In order to run, a Delphi application needs a lot of background information, such as how to interact with the operating system and how to create forms and components, such as buttons and edit boxes. This information is contained in various unit files that are a standard part of Delphi. This collection of units is referred to as the RTL (run time library).
Let's begin our examination of the uses list by looking at the auto-generated code:
01: unit Unit1;
02: interface
03: uses
03: Windows, Messages, SysUtils, Variants, Classes,
03: Graphics, Controls, Forms, Dialogs;
04: type
06: TForm1 = class(TForm)
07: private
08: { Private declarations }
09: public
10: { Public declarations }
11: end;
12: var
13: Form1: TForm1;
14: implementation
15: {$R *.dfm}
16: end.
|
When you create a new form (with its associated unit), Delphi places all necessary units in the (interface) uses clause (line: 03). Although you haven't added any components to the form or written any code, you already have a complete GUI application that you can compile and run. All it does is display a blank form.
Note: two more units are also "listed" in the uses list. The System unit and the SysInit unit are used automatically by every application and cannot be listed explicitly in the uses clause. For instance, routines in the System unit implement file I/O, string handling, floating point operations, dynamic memory allocation, and so forth.
Placing components on a form...
Recall that Delphi comes with more than 200 components for you to "drop on a form" (depending on your Delphi version). It's quite natural that all those components are not defined in only 10 units, after all every third-party component (or the one you build on your own) comes "inside" its own unit.
A question: should you need to know the exact name of every unit when you want to use some component? Luckily, no.
Suppose you add a (standard) button component to this form and write an OnClick event handler that changes the Caption of the form when the user clicks the button. The result might look like this:
01: unit Unit1;
02: interface
03: uses
03: Windows, Messages, SysUtils, Variants, Classes,
03: Graphics, Controls, Forms, Dialogs, StdCtrls;
04: type
06: TForm1 = class(TForm)
07: Button1: TButton;
08: procedure Button1Click(Sender: TObject);
09: private
10: { Private declarations }
11: public
12: { Public declarations }
13: end;
14: var
15: Form1: TForm1;
16: implementation
17: {$R *.dfm}
18: procedure TForm1.Button1Click(Sender: TObject);
19: begin
20: //enter code here:
21: Caption := 'Clicked';
22: end;
23: end.
|
What happened? Since the button (TButton) control is defined in the StdCtrls unit, Delphi added the reference to it, in the uses list.
What this means, is that you, in many cases, do not need know the exact name of the unit your unit needs to use. For every (properly installed) component you drop on a form, Delphi will add the name of the associated unit to the uses list!
Note: If you remove that button component, you just dropped on the form Delphi will not remove the reference to the "StdCtrls" unit from the uses list.
Using Delphi's built-in unit files (RTL/VCL routines)
Run Time Library, or VCL routines are (general purpose) functions and procedure that are built into Delphi. A section on this site, RTL reference, provides a through example-reference to the RTL capabilities of Delphi.
For example, the StrUtils unit provides routines for string manipulation; DateUtils includes routines for date manipulation.
Now, suppose you have an edit box (Edit1) on a form. When the user clicks a button, the Caption of the form should change to a reversed string of a string specified in the edit box.
Here's the code:
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption:=ReverseString(Edit1.Text);
end;
|
If you try to compile and run the application, you'll get the following error: Undeclared identifier: 'ReverseString'.
What this means is that the compiler could not find the given identifier (ReverseString) - it might be from another unit that is not listed in the uses clause.
In other words, when using built-in routines, you'll need to know the exact name of the unit in which they are defined.
To fix the above error, you need to manually add the unit "StrUtils" to the uses list.
03: uses
03: Windows, Messages, SysUtils, Variants, Classes, Graphics,
03: Controls, Forms, Dialogs, StdCtrls, StrUtils;
|
But how do you know that Delphi has the ReverseString function? And what if you *know* that there is a function called "MatchesMask", but you simply cannot recall the name of the unit in which the function is defined? One way to help yourself, is to browse through our "Delphi RTL reference" section. Another way is to hit F1 (call Delphi help system) every time an "undeclared identifier..." pops up.
If you "forgot" to add a reference to a unit, the Delphi compiler display an error message and places the cursor at he point in the source code that it cannot process. Usually, you will find there is a routine that is declared in a unit that is not listed in the uses list. Delphi's Help system should tell you which unit you need to add.
Now that you have seen how Delphi maintains a form unit source (and its uses list), and what needs to be done in order to use Delphi's built-in routines, you are ready to start creating your own code units...
Next page > Creating and using your own code units > Page 1, 2, 3
A Beginner's Guide to Delphi Programming: Next Chapter >>
>>
Code navigation