DelphiBasics
$RangeChecks
Compiler Directive
Determines whether Delphi checks array bounds
{$RangeChecks Off}
1
2 {$RangeChecks On}
Description
The $RangeChecks compiler directive determines whether Delphi should add code for array bounds checking.
 
This is set Off by default, meaning that a bad array access will pass unnoticed, revealing itself in a difficult to debug part of the code.
 
It is recommended to switch on $RangeChecks in order to detect array bound problems. This will result in the raising of an exception, allowing code testing to correctlt identify the point of failure.
Notes
$RangeChecks is equivalent to $R.

It can and should only be set once in your code.

The default value is $RangeChecks Off.
Related commands
$RDetermines whether Delphi checks array bounds
$ResourceDefines a resource file to be included in the application linking
 Download this web site as a Windows program.




 
Example code : Trapping array bound problems
var
  myArray : array[1..5] of string;
  i : Integer;

begin
  // Set range checking on
  {$RangeChecks On}

  // Start array assignment from 0 - normally OK, but our
  // array starts at 1.
  for i := 0 to 5 do
  begin
    myArray[i] := 'Element '+IntToStr(i);
    ShowMessage('myArray['+IntToStr(i)+'] = '+myArray[i]);
  end;
end;
Show full unit code
  Delphi throws the ERangeError exception
 
Example code : Ignoring array bounds problems
var
  myArray : array[1..5] of string;
  i : Integer;

begin
  // Set range checking off
  {$RangeChecks Off}

  // Start array assignment from 0 - normally OK, but our
  // array starts at 1.
  for i := 0 to 5 do
  begin
    myArray[i] := 'Element '+IntToStr(i);
    ShowMessage('myArray['+IntToStr(i)+'] = '+myArray[i]);
  end;
end;
Show full unit code
  myArray[0] = Element 0
  myArray[1] = Element 1
  myArray[2] = Element 2
  myArray[3] = Element 3
  myArray[4] = Element 4
  myArray[5] = Element 5
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page