Custom Data Types

 

Custom Pascal Types

 

Enumerations

An enumeration provides a technique of setting a list of integers where each item of the list is ranked and has a specific name. For example, instead of numbers that represent players of a football (soccer) game such as 1, 2, 3, 4, 5, you can use names instead. This would produce GoalKeeper, RightDefender, LeftDefender, Stopper, Libero. The syntax of creating an enumeration is

type Series_Name = (Item1, Item2, Item_n);

In our example, the list of players by their name or position would be

type Players = ( GoalKeeper, RightDefender, LeftDefender, Stopper, Libero );

Once the list has been created, the name you give to the list, such as Players, becomes a variable of its own.

Subranges

A subrange is a group of values created from another, existing, or previously group. The group could be a series of numeric values or alphabetic letters. Otherwise, you must first create the original group of values. The syntax of creating a subrange is:

type RangeName = Low..High;

The type keyword, the equal sign, the double period and the semi-colon are required. The RangeName must be a valid name of an Pascal identifier. The Low parameter must be the value that will determine the beginning of the range of values. The High parameter represents the highest value of the series. For example, you can create a series of number from18 to 25 as follows:

type AdultMemberAge = 18..25;

Alternatively, you can create a subrange based on an enumerator. Here is an example:

type Players = ( GoalKeeper,
                 RightDefender, Stopper, Libero, LeftDefender,
                 MiddleLeft, MiddleCenterLeft, MiddleCenterRight, MiddleRight,
                 Forward1, Forward2 );
type AdultMemberAge = 18..25;
type MiddleCourt = MiddleLeft..MiddleRight;

Sets

Consider a list of numbers as follows: 4, 22, 2378, 705, 291, 73, 255, 808, 1274, 237
Let's call this list A. Imagine that, for any reason, you want to create a "sub-list" of numeric values. But, instead of creating a new list from scratch, you want the items of this "sub-list" to be retrieved from an original list, such as the above A list. Put it another way, in high school, you probably learned that natural numbers are those entire numbers with no decimal places. This means that whenever you create a list of natural numbers such as the above, you are simply creating a "sub-list" of numbers from the natural number N.

A set is a collection of values whose items are taken from a known, existing, defined, and recognizable list or collection of items. The items that are part of a set are called its element or its members. The main rule is that the values must be of the same type but they do not have to be organized in any way or follow a strict rule (but they must be of the same type.


Before defining a set, first decide where its items would come from. The syntax of creating a set is:

set of TypeOfSet;

The set and the of keywords area required to let the compiler know that you are creating a set. If you are creating a set based on a known series, you can specify it using the Low..High combination as we saw with subranges. Here is an example:

type Between12And18 = set of 12..18;

Otherwise, you must first declare the list of values and then derive a set from it

type Players = ( GoalKeeper,
                 RightDefender, Stopper, Libero, LeftDefender,
                 MiddleLeft, MiddleCenterLeft, MiddleCenterRight, MiddleRight,
                 Forward1, Forward2 );
Defense = set of RightDefender..LeftDefender;

 

 

Techniques of Using Variables

The Type Definition

Some of the variables we will use in Object Pascal can be simplified and reduced to a new one-word data type. Object Pascal allows you to define a new data type using the type data type. In this case, type does not create a new kind of data type; instead, it allows you rename an existing data as you see fit and use the new type in your program. The syntax of customizing the name of an existing data type is:

type NewName = KnownDataType;

The type keyword is required to let the compiler know that you are defining a new data type. The NewName must be a valid name for an Object Pascal object. The KnownDataType is any of those we have seen so far. Here is an example:

type NaturalNumber = Integer;

In this case, NaturalNumber becomes a name for an Integer data type. After this, NaturalNumber can be used exactly as if you were using an Integer. Here is an example that redefines an integer data type:

type NaturalNumber = Integer;
var
    NumberOfStudents: NaturalNumber;

Constants

A constant is a value that does not change. There are three main categories of constants you will be using in your programs: those that already exist, those built in the Object Pascal language, and those you will create yourself.

The algebraic numbers you have used in the past are constants because they never change. Examples of constant numbers are 12, 0, 1505, or 88146. Therefore, any number you can think of is a constant. Every letter of the alphabet is a constant and is always the same. Examples of constant letters are d, n, c. Some characters on your keyboard represent symbols that are neither letters nor digits. These are constants too. Examples are #, &, |, !. To simply display a constant on a console window, you can type it in the parentheses of the Write or Writeln procedure. Here is an example:

program Greeting;

{$APPTYPE CONSOLE}

begin
    Writeln('a');
    Writeln(248);
    Write('Press any key to continue...');
    Readln;
end.

This would produce:

a
248
Press any key to continue...

The Object Pascal language defines some constants that are part of the language. Examples are True, False, or nil.

Some values would be constant by default, but their constancy sometimes depends on the programmer. For example, one programmer can define a constant PI as 3.14. Another programmer can decide that the constant PI would be 3.14159. Therefore, you will be defining your own constant values as you see fit for the goal you are trying to achieve.

The safest technique of using a constant is to give it a name. This allows you to manage it from one standpoint. For example, if you plan to use a number such as 3.14 that represents PI, you can simply use the constant 3.14. Imagine you want to use 3.14 in various sections of the program such as in different functions. If you decide to change the number from 3.14 to 3.14159 or another value, you would have to find every mention of 3.14; this can lead to a programming error. The alternative is to declare a variable and assign it the desired value. The new and defined value is called a constant.

To define a constant, type the const keyword following by a name for the constant. Although in the next lesson we will learn about operators, a constant must be initialized before it can be used. Therefore, a constant should be defined as

const ConstantName = DesiredValue;

The const keyword is required to inform the compiler that you are creating a constant. The ConstantName specifies the name of the constant value. It follows the rules we have applied for the names of variables. The DesiredValue is the value that the constant will hold.

A constant can be initialized with any normal value or an expression. The compiler will find out what type of value the constant holds. Once a constant has been defined, it can be used in the program where its name would be substituted for its value.

 


Previous Copyright © 2004 FunctionX Next