gpt4 book ai didi

delphi - TStringGrid 单元格的范围检查是否有效?

转载 作者:行者123 更新时间:2023-12-03 05:16:39 26 4
gpt4 key购买 nike

下面是一个简单的 Delphi 表单应用程序的代码,该应用程序设置超出包含单元格的指定 TStringGrid 范围的单元格值。

运行程序并单击显示表单上的结果网格,当计数器i超过 1 时,应该会生成运行时间范围检查错误。

在项目选项中启用了范围检查,并且我尝试过使用或不使用 {R+} 编译器指令来运行该程序。

为什么没有范围检查错误?

我使用的是在 Windows 7(64 位)上运行的 Delphi7。

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.

最佳答案

来自documentation (添加强调):

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 单元格引用不属于需要进行范围检查的变量和赋值类型。

关于delphi - TStringGrid 单元格的范围检查是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com