This is where the coding begins. I will explain the default source of each form and what every part of it is used for.
We've created two forms in the
part 2 of the tutorial series. Now open the "Hello world" project we've been working on. To do this, simply double-click the "Hello.dpr" file or press
CTRL+O in Delphi. Don't remember to close any other project (if opened) with
File > Close All. Select the first form and go to its source (
F12). You will see this:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
public
end;
var
Form1: TForm1;
implementation
end.
This is a default code for each form you create. Let's have a look at its basic sections.
1 - "interface". This is just a directive, used to describe the interface of your unit. It means that there's no actual coding yet, there's only definitions.
2 - The "uses" clause. Here we can list all the additional units that this unit will be using. As you see, there is Windows, Messages etc. listed already. That is because your form will most likely use some common functions, that are coded in these units. Units (pas files) are not necessary forms - they can just be collections of some functions, visual components (like buttons, text fields etc.) or something else.
3 - "type". Every component in Delphi is a
class. In objective programming, a class is something, that describes a collection of things (just like in real life). For example, a "student" class, a "car" class, "food" class. In this case, our class is called "TForm" (you guessed it - a form class). Type defines a class in this unit. Currrently it defines a class "TForm1", which is a subclass of "TForm" (TForm1 = class(TForm)).
3.1 - "private". Everything, listed here is private to that class and cannot be accessed from other classes. For example, if we want to have a student's name stored anywhere and we don't want the "food" class to have access to it, we define that name in the "private" clause.
3.2 - "public". That's right - everything here is available to public eyes and other classes.
3.3 - "end;". Ends new type definition.
4 - "var". Var directive describes variables. In this case we have a variable, named "Form1", that has a type of "TForm1". So what we really have here is an object of a class "TForm1". Objects are individual realisations of classes. For example if we have a class "student" - it describes every student possible, but we can create an object "john doe" to describe one student only. That's exectly what is done in this case - we have an object "Form1", that belongs to class "TForm1" and describes our main window.
5 - "implementation". Implementation directive tells Delphi that here is the place, where the coding starts. We had an interface directive for describing things in our unit, and implementation actually implements them.
6 - "{$R *.dfm}". This one is a compiler directive, saying to include a DFM file as a resource in your application. DFM files are visual representations of your forms.
7 - "end." End, followed by a period, actually means - THE END :) You will see later that we'll use lots of end, followed by a semicolon, but they only end a block of code, like part
3.3. And an END, followed by a period, defines the end of the unit or an entire program.
So this is the default code, automatically created by Delphi for each form. Now you know what it means, it's time to move to the
next part of our tutorial - variables.