Ex 05
Ex 05
When I introduced data types in Chapter 2, I referred to the fact that in Object Pas-
cal there are both built in data types and type constructors. A simple example of a
type constructor is the enumerated type, covered in that chapter.
The real power of type definition comes with more advanced mechanisms, such as
arrays, records, and classes. In this chapter I'll cover the first two, which in their
essence date back to the early definition of Pascal, but have been changed so much
over the years (and made so powerful) that they barely resemble their ancestral type
constructors with the same name.
Towards the end of the chapter I'll also briefly introduce some advanced Object Pas-
cal data types as pointers. The real power of custom data types, however, will be
unveiled in Chapter 7, where we'll start looking into classes and object-oriented pro-
gramming.
Static Arrays
Traditional Pascal language arrays are defined with a static or fixed size. An example
is in the following code snippets, which defines a list of 24 integers, presenting the
temperatures during the 24 hours of a day:
type
TDayTemperatures = array [1..24] of Integer;
In this classic array definition, you can use a subrange type within square brackets,
actually defining a new specific subrange type using two constants of an ordinal
type. This subrange indicates the valid indexes of the array. Since you specify both
the upper and the lower index of the array, the indexes don’t need to be zero-based,
as it is the case in C, C++, Java, and most other languages (although 0-based arrays
are also quite common in Object Pascal). Notice also that static array indexes in
Object Pascal can be numbers, but also other ordinal types like characters, enumer-
ated types, and more. Non-integral indexes are quite rare, though.
note There are languages like JavaScript that make heavy use of associative arrays. Object Pascal
arrays are limited to ordinal indexes, so you cannot directly use a string as index. There are ready
to use data structures in the RTL implementing Dictionaries and other similar data structures that
provide such features. I'll cover them in the chapter about Generics, in the third part of the book.
Since the array indexes are based on subranges, the compiler can check their range.
An invalid constant subrange results in a compile-time error; and an out-of-range
index used at run-time results in a run-time error, but only if the corresponding
compiler option is enabled.
note This is the Range checking option of the Runtime errors group of the Compiling page of the
Project Options dialog of the IDE. I've already mentioned this option in Chapter 2, in the section
“Subrange Types”.
Using the array definition above, you can set the value of a DayTemp1 variable of the
TDayTemperatures type as follows (and as I've done in the ArraysTest application
project, from which the following code snippets have been extracted):
type
TDayTemperatures = array [1..24] of Integer;
var
DayTemp1: TDayTemperatures;
begin
DayTemp1 [1] := 54;
DayTemp1 [2] := 52;
...
DayTemp1 [24] := 66;