仅关闭一小部分代码的Delphi范围检查

时间:2011-02-14 22:16:20

标签: delphi project-settings range-checking

如何关闭文件的一部分范围检查。关闭很简单,但如何在以后恢复到项目设置?下面的伪代码应该解释它:

Unit1;

//here's range checking on or off as per the project setting

code here...

{$R-}

//range checking is off here because the code causes range check errors

code here...

//now I want to revert to the project setting. How do I do that?

code here...

end.

2 个答案:

答案 0 :(得分:40)

请参阅:IFOPT directive

{$IFOPT R+}
  {$DEFINE RANGEON}
  {$R-}
{$ELSE}
  {$UNDEF RANGEON}
{$ENDIF}
//range checking is off here because the code causes range check errors
//code here...
{$IFDEF RANGEON}
  {$R+}
  {$UNDEF RANGEON}
{$ENDIF}

答案 1 :(得分:4)

将代码包装在$R指令中:

{$R-} // disable range checking
// do non-range-checked operations here
{$R+} // turn range checking back on

请注意,该指令适用于语句级别。你不能用它包装表达式的一部分。