Search code examples
delphiproject-settingsrange-checking

Switch off Delphi range checking for a small portion of code only


How can one switch off range checking for a part of a file. Switching off is easy, but how do I revert to the project setting later on? The pseudo-code below should explain it:

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.

Solution

  • See: 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}