How to: Declare 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 statement in code

Private to the file in which it is declared

To set constants in the Project Designer

To set constants at the command line

  • Use the /d switch to enter conditional compilation constants, as in the following example:

    vbc MyProj.vb /d:conFrenchVersion=–1:conANSI=0

    No space is required between the /d switch and the first constant. 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.

See Also


How to: Collapse and Hide Sections of Code


Conditional Compilation Overview


Conditional Compilation Constants

/define (Visual Basic)

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

#Const Directive

Other Resources

Building from the Command Line (Visual Basic)