2

Below is code for a simple Delphi form application that sets cell values that are out of range for the specified TStringGrid that contains the cells.

Running the program and clicking on the resulting grid on the displayed form should generate a run time range check error when the counter i gets above 1.

Range checking is enabled in the project options, and I have tried running the program with and without the {R+} compiler directive.

Why is there no range check error?

I am using Delphi7 running on Windows 7 (64 bit).

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{$R+} 
procedure TForm1.StringGrid1Click(Sender: TObject);
var
    i : Integer;
begin
    Form1.StringGrid1.ColCount := 2;
    Form1.StringGrid1.RowCount := 3;
    for i := 0 to Form1.StringGrid1.RowCount do begin
        Form1.StringGrid1.Cells[0,i+1] := IntToStr(i);
    end;
end;

end.
Ken White
  • 123,280
  • 14
  • 225
  • 444
Jay Elston
  • 1,978
  • 1
  • 19
  • 38
  • 1
    Range checking is for array access, and string element access. It does not apply to properties. You are accessing a property. – David Heffernan Jul 30 '17 at 19:41
  • David -- that is a good answer! – Jay Elston Jul 30 '17 at 19:51
  • 1
    If you add the line **Form1.StringGrid1.RowCount := 6;** *after* the loop, you will see that the cells have indeed been set, showing that the Cells property is totally independent of RowCount or ColCount, presumably following Delphi guidelines that with properties you don't know what order they are processed in and should organise your code accordingly. This in no way detracts from other comments, it is just an observation that RowCount and ColCount relate only to the visual aspects of the grid. – Dsm Jul 31 '17 at 08:14

1 Answers1

2

From the documentation (emphasis added):

The $R directive enables or disables the generation of range-checking code. In the {$R+} state, all array and string-indexing expressions are verified as being within the defined bounds, and all assignments to scalar and subrange variables are checked to be within range. If a range check fails, an ERangeError exception is raised (or the program is terminated if exception handling is not enabled).

TStringGrid cells references are not among the types of variables and assignments that are subject to range checking.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    Well, they can of course be range-checked too, but that is not automatic RTL range checking as governed by `{$R+}`. – Rudy Velthuis Jul 31 '17 at 08:54
  • @Rudy: Which is exactly what the question asked (about the `{$R}` directive), and what my answer addressed. What's your point? Of course, steak can be broiled, grilled, or ground up for burgers, but none of that has any more relevance than your comment. – Ken White Jul 31 '17 at 14:43
  • Your statement, as such, is not entirely correct. Hence my comment. – Rudy Velthuis Jul 31 '17 at 15:01
  • @RudyVelthuis: Nonsense. The quote is specifically about the effects of `{$R}`, and so is the paragraph I wrote. My statement is absolutely correct in the context in which it is written. TStringGrid cell references are NOT among the types of variables and assignments that are subject to range checking **which is controlled by `{$R}`** – Ken White Jul 31 '17 at 15:17
  • I disagree. It is not nonsense. Range checking can also be done "manually". People tend to forget that. – Rudy Velthuis Jul 31 '17 at 15:25
  • @RudyVelthuis: But **this question** and the **answer I wrote to it** are **specifically related to `{$$}`, not manual range checking. Your comment is as much nonsense as what I wrote about steak above; in the context of **this question and answer**, it's not relevant. – Ken White Jul 31 '17 at 15:34
  • I still disagree. The comment is certainly not nonsense, and the fact that there is no automatic range checking does not mean there can't be, e,g. a range check error or that the range checking could not be dependent on the state of $R. – Rudy Velthuis Jul 31 '17 at 15:41
  • 1
    Ken -- Once you pointed out {$R} is a compile time directive, and _Cells_ is a property, not an array, I gave myself a dope-slap. The compiler has all the information it needs to add code to perform run-time range checking, but does not have the information needed to do this on arbitrary collection properties. Duh. – Jay Elston Jul 31 '17 at 16:12