DelphiBasics
$RangeChecks
Compiler Directive
Determines whether Delphi checks array bounds
1   {$RangeChecks Off}
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
$R Determines whether Delphi checks array bounds
$Resource Defines a resource file to be included in the application linking
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 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
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide 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 2002 - 2017. All rights reserved.  |  Home Page