Starts the definition of fixed data values
1 keyword Const(Const
  Name1 = Expression1;
2 Const
  Name1 Type = Expression1;
3 Name (Const Const1 : type {;...});
The Const keyword is used to start a section of constant definitions.
The section is terminated by the next keyword in a program.
Within the section, one or more constants may be defined. These can be a mixture of normal or typed constants:
1.Normal constants
These give a name Name1 to a fixed expression, Expression1. The expression must resolve into one of the following types:
Integer number
Floating point number
Enumerated value

2.Typed constants
These are very odd. They are constant only in the sense that their value persists all parts of a program. Yet it can be changed (as long as the compiler directive {WriteableConst} is set On).
They are used, for example, when a routine needs to hold values that are preserved across calls.
It is better to use Object Oriented principles to allow data to be preserved across accesses.
3.Constant routine parameters
When passing data to a routine (function or procedure), you can prefix the parameter definition with Const if the value is never updated. This marginally improves performance, clarifies routine operation, and prevents accidental updates of the value.
Related commands
FunctionDefines a subroutine that returns a value
OutIdentifies a routine parameter for output only
ProcedureDefines a subroutine that does not return a value
TypeDefines a new category of variable or process
VarStarts the definition of a section of data variables
 Download this web site as a Windows program.

Example code : Illustrating the different constant types
  MAX_LINES = 3;
  CRUDE_PI  = 22/7;
  HELLO     = 'Hello World';
  LETTERS   = ['A'..'Z', 'a'..'z'];
  DECISION  = True;

  i : Integer;

  // Display our crude value of Pi
  ShowMessage('Crude Pi = '+FloatToStr(CRUDE_PI));

  // Say hello to the WOrld

  // Display MAX_LINES of data
  for i := 1 to MAX_LINES do
    // Do some checking - note that Char(i+64) = 'A'
    if DECISION and (Char(i+63) in LETTERS)
    then ShowMessage(Char(i+63)+' is a letter')
    else ShowMessage(Char(i+63)+' is not a letter');
Show full unit code
  Crude Pi = 3.14285714285714
  Hello World
  @ is not a letter
  A is a letter
  B is a letter
Example code : Preserving a number value across calls to a routine
  i : Integer;

  for i := 1 to 8 do
    ShowMessage('Number = '+IntToStr(GetNextNumber));

// Get the next number from a given sequence
function TForm1.GetNextNumber: Integer;
  // Define a range of numbers returnable by this routine
  TNumbers = 0..5;

  // A typed constant is really a persistent variable :
  // It's value is preserved across calls to this routine.
  {$WriteableConst On}
  nextNumber : TNumbers = 0;  // Start at the beginning

  // Return the current value
  result := nextNumber;

  // Increment to the next value
  nextNumber := (nextNumber + 1) MOD 6;
Show full unit code
  Number = 0
  Number = 1
  Number = 2
  Number = 3
  Number = 4
  Number = 5
  Number = 0
  Number = 1
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page