Conditional Compilation

by Itay Sagui

Conditional compilation is a feature that allows you to compile only certain parts of the program. These feature allows you to include specific features according to the platform, or changing time and date formats.

The idea behind compilation is that you can give the compiler specific instructions, and thus tell it NOT to compile certain code, or to compile certain part instead of others, if a specific condition is met.

Let's look on how it is done. The first thing to learn about conditional compilation, is the use of #If..Then. Just like normal If..Then statements, you use a boolean condition.

#If conLanguage = 0 Then
     str = "This is the English Version"
#ElseIf conLanguage = 1 Then
    str = "Voila la version Francais"
#End If

As you can see, it is quite simple. But where do we set the value of conLanguage? We've got 3 options. The first one is to use the #Const statement. The scope of the const will be private in the module that it is declared in:

#Const conLanguage = 0

The other options are theses:
1. Using the command-line, like this: App.exe /d conLanguge=0
2. Using the Project properties dialog, under 'Conditiona Compilation Arguments'.
Both of these methods will declare the consts as public to all the project.

Although there may be more, the only predefined constant that I know of is the WIN32, that tells you if you're running on a Win32 machine.

Conditional compilation can add a real boost to both your programming time, and to the speed and size of your applications. For example, while developing a DirectX game, you might want to see the game running in windowed-mode, but when released, it should be in full-screen. By keeping a #Const called InDev, you can have the VB compiler compile just the needed code, and ignore all the other code.