1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Delphi for Beginners:
The basic layout of a Pascal/Delphi program.

Starting with this article I'll try to present fundamentals of Object Pascal Programming.

If you are new to Delphi, consider reading previous features for Delphi Beginners:

  • Why Delphi? (what is Delphi and what can it do for you)
  • Getting Started (an overview of Delphi programming and a simple application)
  • Working With Components (introduction to Delphi VCL)
  • Database Form Wizard (developing my own, first Delphi database application)
  • Visual programming is fun. Unfortunately, event handlers that you've seen, in those articles, didn't do much. To fully understand and take advantage of Delphi, we must become comfortable with the dialect of Object Pascal built into Delphi.

       Statements or Long live the ";"
    A statement in Delphi should be thought as a complete "sentence." Generally, statements describe algorithmic actions that can be executed. Statements do things to Delphi objects. These objects may be properties of components, numeric expressions or some other more exotic gadgets.

    Delphi distinguishes between simple statement and compound statements. A simple statement is one executable line of code (not one line of code in Code Editor). A compound statement is a block of simple statements surrounded by begin/end keywords.

    //simple statement
    Form1.Caption := 'Ha! New caption ';
    //compound statement
      Top := Top - 10;
      Width := Width + 5;
    As you can see, a semicolon (;) is always required to separate two simple statement.

    Comments are lines of text in your code that are there for documentation purposes. Comments do not take up any room in the compiled code. There are three ways to indicate a comment:

    // one-line comment
    {this is also a comment - can be several lines}
    (* this is another comment *)

    Commenting out executable statements to help debug your programs is a common and often necessary technique in programming languages. You'd be surprised how quickly you forget what code you wrote is supposed to do.

       Properties and Values or Long Live the ":="
    To design the look of your application interface, you set the values of object properties using the Object Inspector. This is referred to as making design time settings. On the other hand, resetting properties via code (run time settings) is one of the most common tasks in Delphi. Any property you can set at design time can also be set at runtime by using code. There are also properties that can be accessed only at runtime. These are known as runtime-only properties.

    When we want to change a property setting (value) for a Delphi object we have to place the object's name followed by a period (.) and the name of the property on the left side of the colon equal (:=) combination - this is not some kind of a smiley.

      ObjectName.Property := Value;

    When you need to reset a large number of properties at one time, you probably would not prefer to have to retype the name of the object each time. The with keyword lets you do this by eliminating the need to type the object name over and over again.

    Enough theory. Let's take a look at a real example:

    When program starts

    procedure TForm1.Button1Click(Sender: TObject);
     Form1.Caption := 'Ha! New caption ';
     Button1.Left := Button1.Left - 10;
     with Button1 do begin
       Top := Top - 10;
       Width := Width + 5;

    After few Button clicks

    Variables in Pascal hold informations (values). Variables have to be declared before they can be used. We do this after the var keyword. The var keyword can be used in several places in the code, such as at the beginning of the code of a function or procedure, to declare variables local to the routine, or inside a unit to declare global variables.

    When declaring a variable, we must state its type. The type of a variable defines the set of values the variable can have.

     SomeValue    : Integer; 
     NewAdress    : string;
     IsThisWorthy : boolean;
     GimmeMoney   : Currency;
     Something    : variant;
     A, B, C      : char;
     Lookout      : Pointer;
    As you can see, we can declare more than one variable of the same type by placing a comma between them. (SomeValue is variable of Integer type.)

       Assigning values to variables
    After you declare a variable, you can then use it to manipulate data in memory. Just as for setting properties, Delphi uses the := sign for assigning values to variables. Like:

    GimmeMoney := 323,23; //Curreny type variable
    IsThisWorthy := True; //boolean type
    NewAdress := 'My new home adress'; //string type

    Delphi enforces strict rules on what kind of assignments we can make between variable types. This means that something like
      GimmeMoney := 'one thousand';
    will give "Incompatible Types" error message. We can't assign string value to currency type variable.

    Another example (you should really try this one, it's fun):

    procedure TForm1.Button2Click(Sender: TObject);
    PosL, PosT : Integer;
    TLCap: string;
     // assign value to PosL and PosT variables
     // assign value to TLCap variable
     TLCap:='Position: ';
     TLCap:=TLCap + IntToStr(Button2.Left) + ' - ';
     TLCap:=TLCap + IntToStr(Button2.Top);
     //use variables to change object properties
     with Button2 do begin

    Constants (values that do not change during program execution) are declared using the const keyword. To declare a constant you don't need to specify a data type, but only assign an initial value.

     Min       = 30; 
     Max       = 500;
     Middle    = (Max + Min) div 2;
     ErrStr    = 'Some errors occured';
     MaxAmount = 123.45;  

    Obviously, line of code like
      Middle:= 275;
    will generate an error! ("Left side cannot be assigned to")

    However, following line of code if quite usual/acceptable:

      SomeValue := Middle + 200;

       Final notes
    My idea was to show you some basics of Delphi's built in programming language. Object Pascal is far more complex and can give much more to Delphi developer. In some future articles I'll be writing about Loops and Decisions, Procedures, Functions, Arrays, Custom data types and so on...

    ©2017 About.com. All rights reserved.