Delphi coding hints and tips.

Compiler directives

Using compiler directives it is possible within delphi to get the compiler to include or exclude a chunk of source code from your exe based on the evaluation of conditional symbols.

Conditional symbols are defined and undefined using the compiler directives {$DEFINE name} {$UNDEF name} You can also use the /D switch in the command-line compiler to define a symbol (or add the symbol to the conditional defines input box on the directories/conditionals page of the project|options dialog box in the IDE).

Conditional symbols are best compared to Boolean variables: They are either True (defined) or False (undefined). The {$DEFINE} directive sets a given symbol to True, and the {$UNDEF} directive sets it to False.

Here's an example of defining a conditional symbol:

{$DEFINE DEBUG}

And here's how you could use it in your code by calling OutputDebugString and sending a string to the event log in Delphi v5:

{$IFDEF DEBUG}OutputDebugString('Here');{$ENDIF}