RAD Studio (Common)
ContentsIndex
PreviousUpNext
Declared Constants

Several different language constructions are referred to as 'constants'. There are numeric constants (also called numerals) like 17, and string constants (also called character strings or string literals) like 'Hello world!'. Every enumerated type defines constants that represent the values of that type. There are predefined constants like True, False, and nil. Finally, there are constants that, like variables, are created individually by declaration. 

Declared constants are either true constants or typed constants. These two kinds of constant are superficially similar, but they are governed by different rules and used for different purposes.

A true constant is a declared identifier whose value cannot change. For example,

const MaxValue = 237;

declares a constant called MaxValue that returns the integer 237. The syntax for declaring a true constant is 

const identifier = constantExpression 

where identifier is any valid identifier and constantExpression is an expression that the compiler can evaluate without executing your program. 

If constantExpression returns an ordinal value, you can specify the type of the declared constant using a value typecast. For example

const MyNumber = Int64(17);

declares a constant called MyNumber, of type Int64, that returns the integer 17. Otherwise, the type of the declared constant is the type of the constantExpression.

  • If constantExpression is a character string, the declared constant is compatible with any string type. If the character string is of length 1, it is also compatible with any character type.
  • If constantExpression is a real, its type is Extended. If it is an integer, its type is given by the table below.
 

Types for integer constants

Range of constant(hexadecimal)  
Range of constant(decimal)  
Type  
-$8000000000000000..-$80000001  
-2^63..-2147483649  
Int64  
-$80000000..-$8001  
-2147483648..-32769  
Integer  
-$8000..-$81  
-32768..-129  
Smallint  
-$80..-1  
-128..-1  
Shortint  
0..$7F  
0..127  
0..127  
$80..$FF  
128..255  
Byte  
$0100..$7FFF  
256..32767  
0..32767  
$8000..$FFFF  
32768..65535  
Word  
$10000..$7FFFFFFF  
65536..2147483647  
0..2147483647  
$80000000..$FFFFFFFF  
2147483648..4294967295  
Cardinal  
$100000000..$7FFFFFFFFFFFFFFF  
4294967296..2^63–1  
Int64  

Here are some examples of constant declarations:

const
  Min = 0;
  Max = 100;
  Center = (Max - Min) div 2;
  Beta = Chr(225);
  NumChars = Ord('Z') - Ord('A') + 1;
  Message = 'Out of memory';
  ErrStr = ' Error: ' + Message + '. ';
  ErrPos = 80 - Length(ErrStr) div 2;
  Ln10 = 2.302585092994045684;
  Ln10R = 1 / Ln10;
  Numeric = ['0'..'9'];
  Alpha = ['A'..'Z', 'a'..'z'];
  AlphaNum = Alpha + Numeric;

 

Constant Expressions

A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. Constant expressions cannot include variables, pointers, or function calls, except calls to the following predefined functions:

Abs  
High  
Low  
Pred  
Succ  
Chr  
Length  
Odd  
Round  
Swap  
Hi  
Lo  
Ord  
SizeOf  
Trunc  

This definition of a constant expression is used in several places in Delphi's syntax specification. Constant expressions are required for initializing global variables, defining subrange types, assigning ordinalities to values in enumerated types, specifying default parameter values, writing case statements, and declaring both true and typed constants. 

Examples of constant expressions:

100
'A'
256 - 1
(2.5 + 1) / (2.5 - 1)
'Borland' + ' ' + 'Developer'
Chr(32)
Ord('Z') - Ord('A') + 1

 

Resource Strings

Resource strings are stored as resources and linked into the executable or library so that they can be modified without recompiling the program. 

Resource strings are declared like other true constants, except that the word const is replaced by resourcestring. The expression to the right of the = symbol must be a constant expression and must return a string value. For example,

resourcestring
  CreateError = 'Cannot create file %s'; 
  OpenError = 'Cannot open file %s';  
  LineTooLong = 'Line too long';
  ProductName = 'CodeGear Rocks';
  SomeResourceString = SomeTrueConstant;

Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions. 

Declare a typed constant like this: 

const identifier: type = value 

where identifier is any valid identifier, type is any type except files and variants, and value is an expression of type. For example,

const Max: Integer = 100;

In most cases, value must be a constant expression; but if type is an array, record, procedural, or pointer type, special rules apply.

Array Constants

To declare an array constant, enclose the values of the array's elements, separated by commas, in parentheses at the end of the declaration. These values must be represented by constant expressions. For example,

const Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); 

declares a typed constant called Digits that holds an array of characters. 

Zero-based character arrays often represent null-terminated strings, and for this reason string constants can be used to initialize character arrays. So the previous declaration can be more conveniently represented as

const Digits: array[0..9] of Char = '0123456789';

To define a multidimensional array constant, enclose the values of each dimension in a separate set of parentheses, separated by commas. For example,

type TCube = array[0..1, 0..1, 0..1] of Integer;
const Maze: TCube = (((0, 1), (2, 3)), ((4, 5), (6,7)));

creates an array called Maze where

Maze[0,0,0] = 0
Maze[0,0,1] = 1
Maze[0,1,0] = 2
Maze[0,1,1] = 3
Maze[1,0,0] = 4
Maze[1,0,1] = 5
Maze[1,1,0] = 6
Maze[1,1,1] = 7

Array constants cannot contain file-type values at any level.

Record Constants

To declare a record constant, specify the value of each field - as fieldName: value, with the field assignments separated by semicolons - in parentheses at the end of the declaration. The values must be represented by constant expressions. The fields must be listed in the order in which they appear in the record type declaration, and the tag field, if there is one, must have a value specified; if the record has a variant part, only the variant selected by the tag field can be assigned values. 

Examples:

type
  TPoint = record
     X, Y: Single;
  end;
  TVector = array[0..1] of TPoint;
  TMonth = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
  TDate = record
    D: 1..31;
    M: TMonth;
    Y: 1900..1999;
  end;
const
  Origin: TPoint = (X: 0.0; Y: 0.0);
  Line: TVector = ((X: -3.1; Y: 1.5), (X: 5.8; Y: 3.0));
  SomeDay: TDate = (D: 2; M: Dec; Y: 1960);

Record constants cannot contain file-type values at any level.

Procedural Constants

To declare a procedural constant, specify the name of a function or procedure that is compatible with the declared type of the constant. For example,

function Calc(X, Y: Integer): Integer;
begin
  ...
end;
 
type TFunction = function(X, Y: Integer): Integer;
const MyFunction: TFunction = Calc;

Given these declarations, you can use the procedural constant MyFunction in a function call:

I := MyFunction(5, 7)

You can also assign the value nil to a procedural constant.

Pointer Constants

When you declare a pointer constant, you must initialize it to a value that can be resolved at least as a relative address at compile time. There are three ways to do this: with the @ operator, with nil, and (if the constant is of type PChar or PWideChar) with a string literal. For example, if I is a global variable of type Integer, you can declare a constant like

const PI: ^Integer = @I;

The compiler can resolve this because global variables are part of the code segment. So are functions and global constants:

const PF: Pointer = @MyFunction;

Because string literals are allocated as global constants, you can initialize a PChar constant with a string literal:

const WarningStr: PChar = 'Warning!';
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!