Const Directive


#Const Directive

Syntax

     #Const constantName = expression 


constantName (required)

Name of the compiler constant


expression (required; literal expression)

Any combination of literal values, other conditional compiler constants defined with the #Const directive, and arithmetic or logical operators except Is and IsNot

Description

The #Const directive defines a conditional compiler constant. By using compiler constants to create code blocks that are included in the compiled application only when a particular condition is met, you can create more than one version of the application using the same source code. This is a two-step process.

  1. Define the conditional compiler constant through the #Const directive, the project properties, or command-line compiler switches (see Appendix H).

  2. Evaluate the constant with a #If...Then directive block. If a particular compiler constant is referenced in a #If...Then directive but is not defined, it has value of Nothing.

A conditional compiler constant can be assigned any string, numeric, or logical value returned by an expression. The expression can only consist of literals, operators other than Is and IsNot, and other conditional compiler constants.

When the compiler evaluates the condition of a #If...Then directive, its block of code is compiled as part of the application only when the evaluated expression is true.

Usage at a Glance

  • You can use any arithmetic or logical operator in the expression except Is and IsNot.

  • You cannot use constants defined with the standard Const statement in the expression.

  • You cannot use regular Visual Basic functions or variables in the expression.

  • Setting a compiler constant to Nothing is the same as not defining it.

  • Constants defined with #Const can only be used in conditional code blocks, not in standard VB code.

  • You can place the #Const directive anywhere within a source file. If placed outside of all types, the defined constant is visible throughout the source file, but it is not visible to any other source files in the project. If placed in a type, the scope of the constant is that type. If placed in a procedure, the scope is that procedure and all called procedures.

  • The #Const directive must be the first statement on a line of code. It can be followed only by a comment. The colon, which is used to combine multiple statements on a single logical line, cannot be used with compiler directives.

  • Conditional compiler constants help you debug your code, as well as provide a way to create more than one version of your application. For instance, you can include code that only operates when run in debug mode. The code can be left in your final version and does not compile unless running in the debugger. Therefore, you don't need to keep adding and removing debugging code.

  • Conditional compiler constants may be defined in terms of other conditional compiler constants. In this sample, the Flags constant will have a value of 2.

         #Const Flag1 = 1     #Const Flag2 = 1     #Const Flags = Flag1 + Flag2 

  • A conditional compiler constant can be defined at the command line using the /define or /d switch.

  • Constants defined by #Const are evaluated at compile time and therefore do not return information about the system on which the application is running.

  • The Visual Basic compiler includes several predefined compiler constants for your use. See the "Conditional Compilation Constants" section of Appendix H for a listing of these constants.

See Also

#If...Then...#Else Directive




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net