Udostępnij za pośrednictwem


Conditional Compilation Overview

You can use conditional compilation to select particular sections of code to compile, while excluding other sections. 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 declare a conditional compiler constant in code with the #Const directive, and 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 FrenchVersion Then 
   ' <code specific to the French language version>.
#ElseIf GermanVersion Then 
   ' <code specific to the German language version>.
#Else 
        ' <code specific to other versions>.
#End If

If you set the value of the FrenchVersion 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.

Note

Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.

See Also

Tasks

How to: Declare Conditional Compilation Constants

How to: Collapse and Hide Sections of Code

Reference

#Const Directive

#If...Then...#Else Directives

Other Resources

Building from the Command Line (Visual Basic)