Delphi Data Types
Delphi Data Types
Data is stored in the memory of the computer when the program runs (it can also be stored in a file, but that is another matter
beyond the scope of this tutorial). Each memory 'slot' is identified by a name that the programmer chooses. For
example LineTotal might be used to name a memory slot that holds the total number of lines in a Word Processor document.
The program can freely read from and write to this memory slot. This kind of data is called a Variable. It can contain data such as a
number or text. Sometimes, we may have data that we do not want to change. For example, the maximum number of lines that the
Word Processor can handle. When we give a name to such data, we also give it its permanent value. These are called constants.
Each variable starts with the name you choose, followed by a : and then the variable type. As with all Delphi statements,
a ; terminates the line. As you can see, you can define multiple variables in one line if they are of the same type.
It is very important that the name you choose for each variable is unique, otherwise Delphi will not know how to identify which you
are referring to. It must also be different from the Delphi language keywords. Don't worry, you don't need to come up with an
unusually creative name (such as Me and Ro Jewelry and others in the fashion industry) for each of your variables. Unique in this
case simply means the only occurrence. You'll know when you have got it right when Delphi compiles your code OK (by hitting Ctrl-
F9 to compile).
Delphi is not sensitive about the case (lower or upper) of your names. It treats theCAT name the same as TheCat.
Number types
Delphi provides many different data types for storing numbers. Your choice depends on the data you want to handle. Our Word
Processor line count is an unsigned Integer, so we might choose Word which can hold values up to 65,535. Financial or
mathematical calculations may require numbers with decimal places - floating point numbers.
var
// Integer data types :
Int1 : Byte; // 0 to 255
Int2 : ShortInt; // -127 to 127
Int3 : Word; // 0 to 65,535
Int4 : SmallInt; // -32,768 to 32,767
Int5 : LongWord; // 0 to 4,294,967,295
Int6 : Cardinal; // 0 to 4,294,967,295
Int7 : LongInt; // -2,147,483,648 to 2,147,483,647
Int8 : Integer; // -2,147,483,648 to 2,147,483,647
Int9 : Int64; // -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Text types
Like many other languages, Delphi allows you to store letters, words, and sentences in single variables. These can be used to
display, to hold user details and so on. A letter is stored in a single character variable type, such as Char, and words and sentences
stored in string types, such as String.
var
Str1 : Char; // Holds a single character, small alphabet
Str2 : WideChar; // Holds a single character, International alphabet
Str3 : AnsiChar; // Holds a single character, small alphabet
Str4 : ShortString; // Holds a string of up to 255 Char's
Str5 : String; // Holds strings of Char's of any size desired
Str6 : AnsiString; // Holds strings of AnsiChar's any size desired
Str7 : WideString; // Holds strings of WideChar's of any size desired
Some simple text variable useage examples are given below - fuller details on strngs and characters is given in the Text tutorial.
var
Log1 : Boolean; // Can be 'True' or 'False'
Boolean variables are a form of enumerated type. This means that they can hold one of a fixed number of values, designated by
name. Here, the values can be True or False. See the tutorials on Logic and Looping for further details.
type
TSuit = (Hearts, Diamonds, Clubs, Spades); // Defines the enumeration
var
suit : TSuit; // An enumeration variable
Sets are often confused with enumerations. The difference is tricky to understand. An enumeration variable can have only one of
the enumerated values. A set can have none, 1, some, or all of the set values. Here, the set values are not named - they are simply
indexed slots in a numeric range. Confused? Well, here is an example to try to help you out. It will introduce a bit of code a bit
early, but it is important to understand.
type
TWeek = Set of 1..7; // Set comprising the days of the week, by number
var
week : TWeek;
begin
week := [1,2,3,4,5]; // Switch on the first 5 days of the week
end;
See the Set reference, and the Sets and enumerations tutorial for further details. That tutorial introduces a further data type -
a subrange type.
Note that we use upper case letters to identify constants. This is just a convention, since Delphi is not case sensitive with names (it
is with strings). Note also that we use = to define a constant value.
types
TWeek = 1..7; // Set comprising the days of the week, by number
TSuit = (Hearts, Diamonds, Clubs, Spades); // Defines an enumeration
const
FRED = 'Fred'; // String constant
YOUNG_AGE = 23; // Integer constant
TALL : Single = 196.9; // Decimal constant
NO = False; // Boolean constant
var
FirstName, SecondName : String; // String variables
Age : Byte; // Integer variable
Height : Single; // Decimal variable
IsTall : Boolean; // Boolean variable
OtherName : String; // String variable
Week : TWeek; // A set variable
Suit : TSuit; // An enumeration variable
These allow programmers to group together variables, and treat this group as a single variable. When we discuss programming
logic, you will see how useful this can be.
Arrays
Array collections are accessed by index. An array holds data in indexed 'slots'. Each slot holds one variable of data. You ca n
visualise them as lists. For example:
var
Suits : array[1..4] of String; // A list of 4 playing card suit names
begin
Suits[1] := 'Hearts'; // Assigning to array index 1
Suits[2] := 'Diamonds'; // Assigning to array index 2
Suits[3] := 'Clubs'; // Assigning to array index 3
Suits[4] := 'Spades'; // Assigning to array index 4
end;
The array defined above has indexes 1 to 4 (1..4). The two dots indicate a range. We have told Delphi that the array elements will
be string variables. We could equally have defined integers or decimals.
Records
Records are like arrays in that they hold collections of data. However, records can hold a mixture of data types. Ther are a very
powerful and useful feature of Delphi, and one that distinguishes Delphi from many other languages.
Normally, you will define your own record structure. This definition is not itself a variable. It is called a data type (see Types for
further on this). It is defined in a type data section. By convention, the record type starts with a T to indicate that it is a type not
real data (types are like templates). Let us define a customer record:
type
TCustomer Record
firstName : string[20];
lastName : string[20];
age : byte;
end;
Note that the strings are suffixed with [20]. This tells Delphi to make a fixed space for them. Since strings can be a variable length,
we must tell Delphi so that it can make a record of known size. Records of one type always take up the same memory space.
Let us create a record variable from this record type and assign to it:
var
customer : TCustomer; // Our customer variable
begin
customer.firstName := 'Fred'; // Assigning to the customer record
customer.lastName := 'Bloggs';
customer.age := 55;
end;
customer.firstName is now set to 'Fred'
customer.lastName is now set to 'Bloggs'
customer.age is now set to 55
Notice how we do not use an index to refer to the record elements. Records are very friendly - we use the record element by its
name, separated from the record name by a qualifying dot. See the Records tutorial for further on records.
Objects
Objects are collections of both data and logic. They are like programs, but also like data structures. They are the key part of
the Object oriented nature of Delphi. See the Object orientation tutorial for more on this advanced topic.
Files
File variables represent computer disk files. You can read from and write to these files using file access routines. This is a complex
topic covered in Files.
Pointers
Pointers are also the subject of an advanced topic - see Pointer reference. They allow variables to be indirectly referenced.
Variants
Variants are also an advanced topic - see Variant. They allow the normal Delphi rigid type handling to be avoided. Use with care!
Type definitions
When we discussed Records above, we introduced the concept of types. Delphi has many predefined data types - both simple, such
as string, and compound, such as TPoint (which holds X and Y coordinates of a point). See Type for further details.