|
Page 2 of 2
.
.
Conditional Compilation
Conditional compilation can be used to select particular sections of code to compile while excluding other sections. For example, debugging statements may have to be written, that compare the speed of different approaches to the same programming task, or an application may have to be localized for multiple languages. Conditional compilation statements are designed to run during compile time, not at run time.
A conditional compiler constant can be declared in code with the #Const directive, and blocks of code can be denoted 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, platform-specific code segments will have to be embedded in #If...Then statements using the predefined constants FrenchVersion and GermanVersion. The following example demonstrates the point:
#If FrenchVersion Then
' < code specific to the French language version >.
#ElseIf GermanVersion Then
' < code specific to the German language version >.
#Else
' .
#End If
If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch. These constants can be used only for conditional compilation; they cannot be used in executable code. This example uses the TARGET conditional compilation constant to determine whether to compile certain statements. The Visual Studio integrated development environment defines the CONFIG, DEBUG, and TRACE conditional compilation constants. The Visual Basic compiler defines the TARGET and VBC_VER conditional compilation constants. The TARGET and VBC_VER constants are not available in compiler versions before Visual Basic 2005
Parts
Expression
Required for #If and #ElseIf statements, optional elsewhere. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False.
Statements
Required for #If statement block, optional elsewhere. Visual Basic program lines or compiler directives that are compiled if the associated expression evaluates to True.
#End If
Terminates the #If statement block.
Remarks
On the surface, the behavior of the #If...Then...#Else directives appears the same as that of the If...Then...Else statements. However, the #If...Then...#Else directives evaluate what is compiled by the compiler, whereas the If...Then...Else statements evaluate conditions at run time.
Conditional compilation is typically used to compile the same program for different platforms. It is also used to prevent debugging code from appearing in an executable file. Code excluded during conditional compilation is completely omitted from the final executable file, so it has no effect on size or performance.
Regardless of the outcome of any evaluation, all expressions are evaluated using Option Compare Binary. The Option Compare statement does not affect expressions in #If and #ElseIf statements.
It must be noted that no single-line form of the #If, #Else, #ElseIf, and #End If directives exists. No other code can appear on the same line as any of the directives.
Example
This example uses the #If...Then...#Else construct to determine whether to compile certain statements.
#Const CustomerNumber = 36
#If CustomerNumber = 35 Then
' Insert code to be compiled for customer # 35.
#ElseIf CustomerNumber = 36 Then
' Insert code to be compiled for customer # 36.
#Else
' Insert code to be compiled for all other customers.
#End If
#End If If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
#End If If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
#End If If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
#End IfIf the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
Conditional Compilation Constants
Conditional compilation allows the user control at compile time what code to include in the program.
The following table lists the predefined constants available for conditional compilation.
|
Constant
|
Description
|
|
CONFIG
|
A string that corresponds to the current setting of the Active Solution Configuration box in the Configuration Manager.
|
|
DEBUG
|
A Boolean value that can be set in the Project Properties dialog box. By default, the Debug configuration for a project defines DEBUG. When DEBUG is defined, System.Diagnostics.Debug class methods generate output to the Output window. When it is not defined, System.Diagnostics.Debug class methods are not compiled and no Debug output is generated.
|
|
TARGET
|
A string representing the output type for the project or the setting of the command-line /target option. The possible values of TARGET are: "winexe" for a Windows application, "exe" for a console application, "library" for a class library, and "module" for a module. The /target option may be set in the Visual Studio integrated development environment. For more information, see /target.
|
|
TRACE
|
A Boolean value that can be set in the Project Properties dialog box. By default, all configurations for a project define TRACE. When TRACE is defined, System.Diagnostics.Trace class methods generate output to the Output window. When it is not defined, System.Diagnostics.Trace class methods are not compiled and no Trace output is generated.
|
|
VBC_VER
|
A number representing the Visual Basic version, in major.minor format. The version number for Visual Basic 2005 is 8.0.
|
These constants can be used only for conditional compilation; they cannot be used in executable code.
Example
This example uses the TARGET conditional compilation constant to determine whether to compile certain statements.
#If TARGET = "winexe" Then
' Insert code to be compiled for a Windows application.
#ElseIf TARGET = "exe" Then
' Insert code to be compiled for a console application.
#End If
Requirements
The Visual Studio integrated development environment defines the CONFIG, DEBUG, and TRACE conditional compilation constants.
The Visual Basic compiler defines the TARGET and VBC_VER conditional compilation constants. The TARGET and VBC_VER constants are not available in compiler versions before Visual Basic 2005
#If...Then...#Else Directives
Conditionally compiles selected blocks of Visual Basic code.
#If expression Then
statements
[ #ElseIf expression Then
[ statements ] ]
...
#ElseIf expression Then
[ statements ] ]
[ #Else
[ statements ] ]
#End If
|
Method
|
Output
|
|
Assert
|
The specified text; or, if none is specified, the Call Stack. The output is written only if the condition specified as an argument in the Assert statement is false.
|
|
Fail
|
The specified text; or, if none is specified, the Call Stack.
|
|
Write
|
The specified text.
|
|
WriteIf
|
The specified text, if the condition specified as an argument in the WriteIf statement is satisfied.
|
|
WriteLine
|
The specified text and a carriage return.
|
|
WriteLineIf
|
The specified text and a carriage return, if the condition specified as an argument in the WriteLineIf statement is satisfied.
|
Trackback(0)

|