#Const Directive VB.NET




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


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 compilerconstants 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 inexpression.
  • 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:
  • 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. For example, the intent of the following code fragment is to test for a sound card and, if one is present, to include code to take advantage of the system's enhanced sound capabilities:
  • However, the code does not work as expected, since it includes or excludes the code supporting a sound card based on the state of the machine on which the program is compiled, rather than the machine on which the application is run.

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics