Conditional Compilation in Visual Basic
In conditional compilation, particular blocks of code in a program are compiled selectively while others are ignored.
For example, you may want to write debugging statements that compare the speed of different approaches to the same programming task, or you may want to localize an application for multiple languages. Conditional compilation statements are designed to run during compile time, not at run time.
You denote blocks of code to be conditionally compiled with the #If...Then...#Else directive. For example, to create French- and German-language versions of the same application from the same source code, you embed platform-specific code segments in #If...Then statements using the predefined constants FrenchVersion and GermanVersion. The following example demonstrates how:
If you set the value of the FrenchVersion conditional compilation constant to True at compile time, the conditional code for the French version is compiled. If you set the value of the GermanVersion constant to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs.
Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
You can set conditional compilation constants in one of three ways:
In the Project Designer
At the command line when using the command-line compiler
In your code
Conditional compilation constants have a special scope and cannot be accessed from standard code. The scope of a conditional compilation constant is dependent on the way it is set. The following table lists the scope of constants declared using each of the three ways mentioned above.
How constant is set
Scope of constant
Public to all files in the project
Public to all files passed to the command-line compiler
#Const statement in code
Private to the file in which it is declared
To set constants in the Project Designer
To set constants at the command line
To set constants in your code
Provides suggestions for making your code easy to read and maintain.