Delphi Programming/Data types

First, let's define a type.

In Delphi, like in C, VB and unlike PHP, Python or other modern languages, a variable is typed and can only contain values of this type. To make it simple, a type is a variable attribute.

Basic types

edit

Delphi is extremely complete for this. There are lots of predefined types and you can also create your own types (we will see it later). Here is a table with the most useful basic types (also called primitive types):

Type Description Size Value
boolean boolean 1 bit True/False
char character 1 byte One item of the ASCII table (or ANSI)
string string of characters Depends on its number of characters Dynamic table of items of the ASCII table (or ANSI)
integer positive or negative integer 4 bytes -2147483648 to 2147483647
cardinal unsigned integer 4 bytes 0 to 4294967295
real (equivalent to double) real number 8 bytes 5.0 x 10^-324 to 1.7 x 10^308
Remarks: Above are only the most popular predefined types. There are dozens of other types just for the integers (smallint, longint, int64...) whose usage fits very specific needs (like the size of the data we will put in) and so optimizes the memory usage. For more information about it, see the documentation in the Delphi help content.

char

edit

A char (a character) is something like '#', 'A', '2', ';', '.', '?' and so on. It has a number in a library of chars, the ASCII. In Delphi, a char is typed between two single quotes. If the given character is the single quote itself, you should double the character, in other words, you have to type four single quotes.

var
  a, b: char;

begin
  a := 'H';  // a = "H"
  b := 'i';  // b = "i"
  WriteLn(a);  // Display "H"
  WriteLn(a, b);  // Display "Hi"
  WriteLn(a, b, '!');  // Display "Hi!"
end.

string

edit

You have already worked with strings in the last chapter, such as 'Hello World' or 'Hi there' and so one. A string is a sequence of chars.

 var
   text1: string;
   character1: char;

And then write between begin and end:

 character1 := 'H';
 text1 := 'ello World!';
 WriteLn(character1, text1);

If you want to write a single quote in a string, the best way is to type two successive single quotes:

 text1 := 'I''m John Doe.';

Strings behave like the tables. You can access to a character using an index:

 text := 'Hello World!';
 WriteLn(text[1]); // Only print "H".

The first index is 1 (and not 0).

Textual expressions

edit

Strings can be linked using the + operator:

 firstWord := 'Hello';
 secondWord := 'World';
 WriteLn(firstWord + ' ' + secondWord + '!');
 wholeText := firstWord + ' ' + secondWord + '!';

The + operator can be used with variables with different types. According to the types, the result will have a different type:

  • string + string → string
  • string + char → string
  • char + char → string
  • char + string → string
  • string + number → error
  • number + string → error
  • number + number → number
  • number + char → error
  • char + number → error

Integers

edit

Integers are even integers, you write:

 a: Integer;

Floats

edit

Floats are decimal numbers. They use a floating point. You can write:

 c: Real;   // Real is normally the same like Single
 a: Single;
 b: Double;
 d: Extended;

An Extended is more precise than a Real, but needs more main storage.

Boolean

edit

Boolean is a logical value. It can be 'True' or 'False'. Write:

 var
   a: Boolean;
 begin
   a := True;
   // or
   a := False;

But there is only a sense of Boolean when you know the conditional structures in the next chapter.

Arrays

edit

An array is a collection of strings, floats, integers or any data type. It can also contain an array. In this case, we have an array with several dimensions. You say:

 a: array [from..to] of data_type;
  a: array [1..3] of string;

for one dimension. For two:

 a: array [fX..tX] of
    array [fY..tY] of data_type;

and so on. A single element of an array is for example:

 a[1] := 'Hello '
 a[2] := 'World'
 a[3] := '!';
WriteLn(a[1],
         a[2],
         a[3]);

Record

edit

A record (also called a structure in other languages) represents a set of heterogeneous data. Each item is a field. The record type declaration specifies the name and the type of each field. By convention, the name of a record type starts with a "T". A record type declaration has the following syntax:

type recordTypeName= record
listField1: type1;
 ...
listFieldn: typen;
end

For example, the following declaration creates a record type called TDateRec.

type
  TDateRec = record
    Year: Integer;
    Month: (Jan, Feb, Mar, Apr, May, Jun,
            Jul, Aug, Sep, Oct, Nov, Dec);
    Day: 1..31;
  end;

Each TDateRec contains three fields: an integer value called Year, an enumerate value called Month and another integer value between 1 and 31 called Day. The identifiers Year, Month and Day are names of fields of TDateRec that behave as variables. However, the TDateRec type declaration doesn't allocate memory for the Year, Month and Day fields. The memory is allocated when you instantiate a record, which you do as follows:

var Record1, Record2: TDateRec;

The variable declaration creates two instances of TDateRec, called Record1 and Record2.

You can access to a record field by qualify the name of the field with the name of the record:

Record1.Year := 1922;
Record1.Month := Nov;
Record1.Day := 26;

Or by using the with instruction:

with Record1 do
begin
  Year := 1922;
  Month := Nov;
  Day := 26;
end;

Examples

edit

The following example asks a question, retrieves the answer and prints the answer:

 var
 _Input, _Linkage: string;
 begin
   WriteLn('Hello, what is your name?');
   ReadLn(_Input);
   _Linkage := 'Hello, ' + _Input;
   WriteLn(_Linkage);
 end.

ReadLn(_Input) means that the application waits for the user to enter a text and then saves the text in the variable _Input.

This other example gets the number of a char in the ASCII:

 var
   number: Integer; // Declare number as an integer
   _Input: string;
   _Char1: char;
 begin
   WriteLn('Enter a char!');
   ReadLn(_Input);
   _Char1 := _Input[1];
   number := Ord(_Char1);
   WriteLn('ASCII is: ', number);
 end.

With _Input[1] you get the first char of _Input. And the first char is char 1, (not char 0 like in C++!)