A constant is an identifier whose value is defined by the programmer and cannot be changed at run time. Constants are declared using the reserved word const. Declarations of constants differ somewhat from variable declarations. When you declare a constant, you don't have to (but you can) specify the type. If you don't specify the data type, the Delphi compiler will automatically align the constant value to the most appropriate data type. Delphi uses the smallest data type that is sufficient to hold the desired constant value. For instance, if we define a constant with the value 128, Delphi will mark the constant as a Byte constant.

In Delphi, we can declare two types of constants: true and typed. True constants are declared like this:

const   Z = 10;   Z2 = Z + 5;

Typed constants are constants with an explicitly defined data type:

const   XX: Integer = 100;

The difference between the two types of constants is that true constants can be used in other constant declarations while typed constants cannot:

const   Z = 10;   Z2 = Z + 5; { OK to use the  true constant Z. } const   XX: Integer = 100;   XX2 = XX + 20; { This is illegal. XX is a typed constant. }

You should declare a constant when you repeat the same value in two or more places in the source code. If you start using constants from the start, you will save yourself a lot of headaches later on. To easily discern variables from constants in your source code you should always write constants using uppercase letters.

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 © 2008-2017.
If you may any questions please contact us: