Darian Miller
Darian Miller

Reputation: 8098

Delphi Region compiler directive - backwards compatible unit files?

Given the desire to use the useful Region compiler directive in unit files, what's the best approach to allow these same units from being used in previous versions of Delphi? There doesn't seem to be a 'decent' approach. (Desired IDE version is Delphi 7)

I like having Regions above the method definitions to hide/display the method definition help and version history comments but I'd have to surround the Region directives with other compiler directives, which kinda kills the 'cleaness' of region compiler directives.

I suppose a preprocessor could be written to comment out all Region/EndRegion directives if using a version of Delphi which doesn't support regions..and add then back for later versions?

I'm not 100% switched to Delphi 2009 and need to support multiple IDE versions.

Upvotes: 5

Views: 4616

Answers (3)

Jacek Krawczyk
Jacek Krawczyk

Reputation: 2194

You can use the following code:

{$IF CompilerVersion >= 17} // The $REGION directive is available since Delphi 2005
{$REGION 'RegionName'}
{$ENDIF} 

// codeblock

{$IF CompilerVersion >= 17} 
{$ENDREGION 'RegionName'}
{$ENDIF}

Upvotes: 2

Stijn Sanders
Stijn Sanders

Reputation: 36850

There are a few Delphi(7) editor-enhancement-components out there, that provide something like this. I think GExperts does.

If you're ok with an impact on your code, you might consider {$I } to split large files.

Upvotes: 1

gabr
gabr

Reputation: 26850

I'm assuming D7 doesn't want to copile {$REGION} or {$ENDREGION}? I only have D2007/9 installed and cannot check that.

Try this:

{$IFDEF undef}{$REGION 'bla'}{$ENDIF}
//some code
{$IFDEF undef}{$ENDREGION}{$ENDIF}

D2009 editor creates a region as expected. D7 should ignore the $REGION/$ENDREGION if 'undef' is not defined.

Upvotes: 12

Related Questions