Const Directive

   
#Const Directive

Syntax

 #Const  constantname   =   expression  
constantname (required; String literal)

Name of the constant

expression (required; literal)

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

Description

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:

  • Defining the conditional compiler constant. This step is optional; conditional compiler constants that are not explicitly defined by the #Const directive, but are referenced in code, default to a value of Nothing.

  • Evaluating the constant in the conditional compiler #If...Then statement block.

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

When the constant is evaluated, the code within the conditional compiler #If... Then block is compiled as part of the application only when the expression using the conditional compiler constant evaluates to True .

Rules at a Glance

  • Conditional compiler constants are evaluated by the conditional compiler #If... Then statement block.

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

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

  • You cannot use intrinsic functions or variables in expression .

  • Constants defined with #Const can only be used in conditional code blocks.

  • You can place the #Const directive anywhere within a source file. If placed outside of all modules, the defined constant is visible throughout the source file, but is not visible to any other source files in the project. If placed in a module, the scope of the constant is that module. 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. Note that the colon , which is used to combine two complete sets of statements onto a single line, cannot be used on lines that contain #Const .

Programming Tips and Gotchas

  • Conditional compiler constants help you debug your code, as well as provide a way to create more than one version of your application. 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. For example, the following code fragment works as expected:

     #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.

    It is important to remember that the constant defined by #Const is evaluated at compile time and therefore does not return information about the system on which the application is running.

See Also

#If

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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