Visual Basic 中的条件编译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.

您可以用指令指示要有条件地编译的代码块 #If...Then...#ElseYou denote blocks of code to be conditionally compiled with the #If...Then...#Else directive. 例如,若要从相同的源代码创建法语和德语版本的同一应用程序,可以 #If...Then 使用预定义的常量和将平台特定的代码段嵌入到语句中 FrenchVersion GermanVersionFor 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>.
        ' <code specific to other versions>.
#End If

如果在 FrenchVersion 编译时将条件编译常量的值设置为 True ,则编译法语版本的条件代码。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. 如果将常量的值设置 GermanVersionTrue ,则编译器将使用德语版本。If you set the value of the GermanVersion constant to True, the compiler uses the German version. 如果两者均未设置为 True ,则最后一个块中的代码将 Else 运行。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.

声明条件编译常量Declaring Conditional Compilation Constants

可以通过以下三种方式之一来设置条件编译常量: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
项目设计器Project Designer 公共到项目中的所有文件Public to all files in the project
命令行Command line 向传递到命令行编译器的所有文件公开Public to all files passed to the command-line compiler
#Const 代码中的语句#Const statement in code 专用于声明它的文件Private to the file in which it is declared
在项目设计器中设置常量To set constants in the Project Designer
-在创建可执行文件之前,请按照 管理项目和解决方案属性中提供的步骤在 "项目设计器" 中设置常量。- Before creating your executable file, set constants in the Project Designer by following the steps provided in Managing Project and Solution Properties.
在命令行中设置常量To set constants at the command line
-使用 -d 开关输入条件编译常量,如以下示例中所示:- Use the -d switch to enter conditional compilation constants, as in the following example:
vbc MyProj.vb /d:conFrenchVersion=–1:conANSI=0
-D 开关和第一个常数之间不需要空格。No space is required between the -d switch and the first constant. 有关详细信息,请参阅 -定义 (Visual Basic) For more information, see -define (Visual Basic).
命令行声明会重写在 项目设计器 中输入的声明,但不会将其删除。Command-line declarations override declarations entered in the Project Designer, but do not erase them. 项目设计器 中设置的参数对于后续编译仍有效。Arguments set in Project Designer remain in effect for subsequent compilations.
在代码本身中编写常量时,没有严格的规则与它们的位置相关,因为它们的作用域是在其中声明它们的整个模块。When writing constants in the code itself, there are no strict rules as to their placement, since their scope is the entire module in which they are declared.
若要在代码中设置常量To set constants in your code
-将常量放置在使用它们的模块的声明块中。- Place the constants in the declaration block of the module in which they are used. 这有助于保持代码的组织和可读性。This helps keep your code organized and easier to read.
TitleTitle 说明Description
程序结构和代码约定Program Structure and Code Conventions 提供使代码易于阅读和维护的建议。Provides suggestions for making your code easy to read and maintain.


#Const 指令#Const Directive

#If...Then...#Else 指令#If...Then...#Else Directives

-define (Visual Basic)-define (Visual Basic)