Data Types & Variables Grade 10

Every value in Delphi has a data type. Variables are the containers that hold these values while your program runs. Constants hold values that never change.

Data Types

Data TypeWhat it storesExamplesQuotes?
IntegerWhole numbers (positive & negative, no decimals)3, -15, 1250No
RealNumbers with decimals (floating point)3.14, -13.24, 125.89No
StringOne or more characters — text'Banana', '23.45', '@!jkl'Single quotes '…'
CharA single character only'S', '!', 'a'Single quotes '…'
BooleanOnly True or FalseTrue, FalseNo

Variables

A variable is a named storage location that holds a value during program execution. Think of it as a labelled box — you can put something in, use it, and swap it out.

Variables must be: DeclaredPopulatedUsed.

Naming Convention — Prefixes

PrefixData TypeExample
iIntegeriAge
rRealrPrice
sStringsName
cCharcGrade
bBooleanbFound

Declaring Variables

Declared in the var section using: variableName : DataType;

Local Declaration (inside a procedure)

Delphi — Local
procedure TForm1.btnClickClick(Sender: TObject);
var
  sName:   string;
  iNumber: integer;
begin
  // code here
end;

Global Declaration (available to all procedures)

Delphi — Global
var
  Form1:   TForm1;
  sName:   string;   // declared here, below existing var block
  iNumber: integer;

Populating (Assigning) Variables

Use the assignment operator := to put a value into a variable.

Delphi
sName   := 'Jannie';
sName   := edtName.Text;        // from a text box
iNumber := 15;
iNumber := sedNum.Value;         // from a spin edit
bFlag   := True;

Initialising Variables

Always Initialise

Set a starting value before using a variable, especially counters and totals — otherwise Delphi may assign a random garbage value.

Delphi
i        := 0;
iTotal   := 0;
sReverse := '';
cLetter  := '';

Using Variables

Delphi
lblFullName.Caption := sName + ' ' + sSurname;
memOut.Lines.Add(sName + ' ' + sSurname);
iAnswer := iNum1 + iNum2;
memOut.Lines.Add(IntToStr(iNum1) + ' + ' + IntToStr(iNum2)
               + ' = ' + IntToStr(iAnswer));

Constants

A constant holds a value that is known before the program runs and cannot change while it runs. Use const instead of var. No data type is needed, and use = not :=.

Delphi
const
  VAT       = 0.15;
  MAX_ITEMS = 100;

// Using a constant — exactly like a variable
rVatAmount := rCostPrice * VAT;
rTotal     := rCostPrice + rVatAmount;

Type Conversion

Components like TEdit only hold strings. You must convert to the right type before using in calculations.

FunctionConvertsExample
StrToInt()String → IntegeriAge := StrToInt(edtAge.Text);
StrToFloat()String → RealrPrice := StrToFloat(edtPrice.Text);
IntToStr()Integer → StringlblOut.Caption := IntToStr(iTotal);
FloatToStr()Real → StringlblOut.Caption := FloatToStr(rAverage);
The Val procedure — converting safely

StrToInt crashes (runtime error) if the text isn't a valid number. The Val procedure converts a string to a number without crashing: it gives back the converted value AND an error code that tells you whether the conversion worked (0 = success, otherwise the position of the first bad character).

Delphi — Val(sourceString, numberResult, errorPosition)
var
  iValue, iCode : Integer;
begin
  Val(edtAge.Text, iValue, iCode);
  if iCode = 0 then
    ShowMessage('Valid number: ' + IntToStr(iValue))
  else
    ShowMessage('Not a valid number!');   // iCode = position of the bad character
end;

Formatting Real Numbers

Delphi
// FloatToStrF(value, format, totalDigits, decimals)
lblPrice.Caption := FloatToStrF(rPrice, ffCurrency, 4, 2);  // → R24.50
lblPrice.Caption := FloatToStrF(rPrice, ffFixed,    4, 2);  // → 24.50

Types of Errors

Error TypeWhen it occursExample
Syntax ErrorCode is typed incorrectly — won't compileMissing semicolon, missing end
Runtime ErrorCrashes while the program is runningDividing by zero
Logical ErrorRuns, but gives wrong answerCalculating average before total, infinite loop