Data Types


When working with data in Delphi, you usually don't work with memory segments where any kind of data can be found or stored. Instead, you usually work with segments of memory that are strictly defined by a data type. A data type can be regarded as a name for a specific type of data — a string, character, integer, etc. A data type defines not only the name but also the memory size, value range, and operations that can be performed on the data.

Delphi has a plethora of data types (the more the merrier), but a vast majority of these are user-defined types. Basic data type categories in Delphi are string, integer, real, character, and Boolean.

String Data Types

We use the string data type when we want to work with a sequence of characters. The default string data type we use in Delphi is called the AnsiString or long string. The Delphi string can be used to hold very long strings up to 2 GB. That's a lot of text! The string data type is actually the only data type you've used so far. A string in Delphi is any value enclosed in single quotation marks.

Along with the AnsiString type, Delphi supports two other string types: the ShortString and the WideString. The ShortString is an old string type used in DOS and Win 3.x, but there are a few of situations where this type can still be utilized. The ShortString cannot hold more than 255 characters, while the WideString can hold a 2 GB string, just like the default AnsiString. The difference between the AnsiString and WideString data types is that the WideString works with 16-bit Unicode characters.

The string data type defines several operators, including the concatenation operator (+) that can be used to concatenate any number of characters and string parts into a final string:

WriteLn('This ' + 'is ' + 'a ' + 'concatenated ' + 'string.');

Integer and Real Data Types

Table 2-2 shows the Delphi integer types. The Integer and Cardinal types give the best performance results.

Table 2-2: Integer types

Type Name

Range

Integer

–2147483648..2147483647

Cardinal

0..4294967295

Shortint

–128..127

Smallint

–32768..32767

Longint

–2147483648..2147483647

Int64

–263..263–1

Byte

0..255

Word

0..65535

Longword

0..4294967295

You can display integer values by passing them directly to the Write/WriteLn statements:

WriteLn(65); WriteLn(-165 + 35);

Real types are used to store floating-point values. Table 2-3 displays the list of real types. The Double data type gives the best performance results.

Table 2-3: Real types

Type Name

Range

Double (Real)

5.0 x 10–324..1.7 x 10308

Single

1.5 x 10–45..3.4 x 1038

Extended

3.6 x 10–4951..1.1 x 104932

Comp

–263+1..263–1

Currency

–922337203685477.5808..922337203685477.5807

When displaying integer data, you can specify the display width of the value. This is useful when you want to display a simple table or format the output. To specify the width, append the display value with a colon and an integer value that defines the width in characters:

Write(1:5); WriteLn(12:5); Write(123:5); WriteLn(1234:5);

The result of this code is displayed in Figure 2-6.

image from book
Figure 2-6: Result of the Write/WriteLn width parameter

When displaying real data, you can add another parameter, DecPlaces, to define how many characters should be displayed. The decimal places parameter is also defined by appending a colon and an integer value that specifies the width in characters. When specified, the decimal spaces value is a portion of the entire width value; thus, writing 8:2 results in the width of six spaces and two decimal places (see Figure 2-7):

image from book
Figure 2-7: Formatting real values

WriteLn(123.4687:8:2);  WriteLn(90.1:8:2);  WriteLn((33.34 + 66.66):8:2);

Character and Boolean Types

There are two basic character types in Delphi: AnsiChar and WideChar. The default character data type is Char, which is identical to AnsiChar. Characters are usually enclosed in single quotation marks, just like strings:

WriteLn('A'); 

Since characters are really integer values that map to a specific position in the character table (ANSI or Unicode), they can be represented by integers, but we have to insert the number symbol (#) in front of the integer value:

WriteLn(#65);

Now that we know how to write character values using their integer representation, we can display text in a new line without using the WriteLn statement. To do that we have to use characters 10 and 13, which are better known as linefeed and carriage return, respectively:

Write('a'); Write(#10#13);  Write('b');

Another way of writing these characters is to combine them into a string and display them with only one Write statement (making sure there are no spaces between the text in single quotation marks and the character values):

Write('a'#10#13'b');

The Boolean data type is used to represent two logical values: True and False. Boolean values can also be represented as an integer where False is 0 and True is 1.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net