共用方式為


Conditional Compilation Constants 

Conditional compilation allows you to easily control at compile time what code to include in your 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, Debug class methods generate output to the Output window. When it is not defined, 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 can 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, Trace class methods generate output to the Output window. When it is not defined, 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.

_MYTYPE

A string representing the type of project being built. This controls which My objects are available in code. For more information, see How My Depends on Project Type.

You can use these constants 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, TRACE, and _MYTYPE conditional compilation constants.

The Visual Basic compiler defines the TARGET and VBC_VER conditional compilation constants. The TARGET, VBC_VER, and _MYTYPE constants are not available in compiler versions before Visual Basic 2005.

See Also

Tasks

How to: Declare Conditional Compilation Constants

Reference

#If...Then...#Else Directives
#Const Directive
/target (Visual Basic)

Concepts

Conditional Compilation Overview
How My Depends on Project Type