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

定义条件编译器常数。Defines conditional compiler constants.






术语Term 定义Definition
symbol 必需。Required. 要定义的符号。The symbol to define.
value 可选。Optional. 指派给 symbol 的值。The value to assign symbol. 如果 value 是一个字符串,它必须放在反斜杠/双引号序列 (\") 内而不只是双引号内。If value is a string, it must be surrounded by backslash/quotation-mark sequences (\") instead of quotation marks. 如果未指定值,则视为 True。If no value is specified, then it is taken to be True.


-define 选项具有与在源文件中使用 #Const 预处理器指令类似的效果,只是使用 -define 定义的常数为公共的且应用于项目中的所有文件。The -define option has an effect similar to using a #Const preprocessor directive in your source file, except that constants defined with -define are public and apply to all files in the project.

可以将由此选项创建的符号同 #If...Then...#Else 指令一起使用,以对源文件进行条件编译。You can use symbols created by this option with the #If...Then...#Else directive to compile source files conditionally.

-d-define 的缩写形式。-d is the short form of -define.

通过使用逗号分隔符号定义,可以用 -define 定义多个符号。You can define multiple symbols with -define by using a comma to separate symbol definitions.

在 Visual Studio 集成开发环境中设置 -defineTo set -define in the Visual Studio integrated development environment
1.在 “解决方案资源管理器” 中选择一个项目。1. Have a project selected in Solution Explorer. 在“项目”菜单上,单击“属性” 。On the Project menu, click Properties.
2.单击“编译” 选项卡。2. Click the Compile tab.
3.单击 “高级”3. Click Advanced.
4.修改“自定义常量” 框中的值。4. Modify the value in the Custom Constants box.


下面的代码定义并使用两个条件编译器常数。The following code defines and then uses two conditional compiler constants.

    ' Vbc /define:DEBUGMODE=True,TRAPERRORS=False test.vb
    Sub mysub()
#If debugmode Then
        ' Insert debug statements here.
         MsgBox("debug mode")
        ' Insert default statements here.
#End If
    End Sub

请参阅See also