Custom Data Types |
|
|
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. 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. 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. |
|
||
Previous | Copyright © 2004 FunctionX | Next |
|