STATEMENTS PROPERTIES VARIABLES
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:
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 begin Top := Top - 10; Width := Width + 5; end |
Comments
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:

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

Variables
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.
var SomeValue : Integer; NewAdress : string; IsThisWorthy : boolean; GimmeMoney : Currency; Something : variant; A, B, C : char; Lookout : Pointer; |
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'; |
Another example (you should really try this one, it's fun):
procedure TForm1.Button2Click(Sender: TObject); var PosL, PosT : Integer; TLCap: string; begin Randomize; // assign value to PosL and PosT variables PosL:=Random(Form1.ClientWidth-Button2.Width); PosT:=Random(Form1.ClientHeight-Button2.Height); // assign value to TLCap variable TLCap:='Position: '; TLCap:=TLCap + IntToStr(Button2.Left) + ' - '; TLCap:=TLCap + IntToStr(Button2.Top); //use variables to change object properties Form1.Caption:=TLCap; with Button2 do begin Left:=PosL; Top:=PosT; end end; |
Constants
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.
const Min = 30; Max = 500; Middle = (Max + Min) div 2; ErrStr = 'Some errors occured'; MaxAmount = 123.45; |
Obviously, line of code like
Middle:= 275; |
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...

