#Const Directive

Named Arguments

No

Syntax

#Const constantname
						=
						expression


constantname

Use: Required

Data Type: Variant (String)

Name of the constant.


expression

Use: Required

Data Type: 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 aren't explicitly defined by the #Const directive but that are referenced in code default to a value of or False.

  • 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 consist only 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 conditional compiler constant evaluates to True.

You may wonder why you should bother having code that is compiled only when a certain condition is met, when a simple If...Then statement could do the same job. The reasons are:

  • You may have code that contains early bound references to objects that are present only in a particular version of the application. You'd want that code compiled only when you know it wouldn't create an error.

  • You may wish to include code that executes only during the debugging phase of the application. It's often wise to leave this code in the application even after the application has been released, so that you can check back over a procedure if an issue arises. However, you don't want the code to be executed in the final application. The answer is to wrap your debugging code in a conditional statement. You can then provide a conditional constant that acts as a switch to turn debugging code on or off, as the example below demonstrates.

  • Although most operations performed with conditional compilation can be replicated with normal If...Then code blocks, conditional compilation reduces the size of the compiled application and thereby the amount of memory required for the application, making for a more efficient application.

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 can't use other constants defined with the standard Const statement in the expression.

  • According to the documentation, you can't use intrinsic functions in the expression; e.g., #Const MY_CONST = Chr(13) is illegal. In most cases, VBA displays a "Compile error : Variable not found" message if you try this. But there are numerous exceptions. For example, the use of the Int function in the following code fragment doesn't produce a compiler error, and in fact, successfully defines a constant ccDefInt whose value is 3:

    #Const ccDefFloat = 3.1417
    #Const ccDefInt = Int(ccDefFloat)

  • When using #Const, you can't use variables to assign the conditional constant a value.

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

  • Constants defined with #Const have scope only within the module in which they are defined; i.e., they are private.

  • You can place the #Const directive anywhere within a module.

  • You can't use the #Const directive to define the same constant more than once within a module. Attempting to do so produces a "Compile Error: Duplicate Definition" error message.

  • Interestingly, you can define the same constant both through the VB or VBA interface (see the second item in Section 7.1.6) and using the #Const directive. In this case, the constant defined through the interface is visible throughout the application, except in the routine in which the #Const directive is used, where the private constant is visible.

  • 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 combines two complete sets of statements onto a single line, can't be used on lines that contain #Const.

Example

#Const ccDebug = 1 'evaluates to true

Function testValue(sValue as String)
       
sValue = UCase(sValue)
testValue = sValue

#If ccDebug Then
   'this code only executes if ccDebug evaluates to true
   Debug.Print sValue
#End If

End Function

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 operates only when run in debug mode. The code can be left in your final version and won't compile unless running in the debugger. Therefore, you don't need to keep adding and removing debugging code.

  • You can also define conditional constants outside of the application's code. In the VBA Editor, enter the conditional compiler constant into the Conditional Compilation Arguments text box on the General tab of the Project Properties dialog. You can reach it by selecting the Project Properties option (where Project is the name that you've assigned to the project) from the Tools menu. In Visual Basic, the Conditional Compilation Arguments text box is found on the Make property sheet of the Project Properties dialog. It can be accessed by selecting the Project Properties option (again, where Project is the name that you've assigned to the project) from the Project menu. In Access, the Conditional Compilation Arguments text box is found on the Advanced property sheet of the Options dialog, which can be accessed by selecting the Options item from the Tools menu. Conditional compiler constants defined in this way are public to the project.

Sidebar 1. Constants Defined Through the VB/VBA Interface

The rules for defining constants in the Conditional Compilation Arguments text box are somewhat different than for constants defined in code using the #Const statement. The value assigned through the VB/VBA interface must be an integer literal; it can't be an expression formed by using multiple literals or conditional constants, along with one or more operators, nor can it be a Boolean value (i.e., True or False). If multiple conditional constants are assigned through the user interface, they are separated from one another by a colon. For instance, the following fragment defines three constants, ccFlag1, ccFlag2, and ccFlag3:

ccFlag1 = 1 : ccFlag2 = 0 : ccFlag3 = 1


  • In many cases, failing to properly define a constant doesn't produce an error message. When this happens (as it does, for instance, when you attempt to assign a variable's value to a constant), the default value of the constant is False. As a result, attempting to assign the value resulting from an invalid expression to a constant can lead to the inclusion of the wrong block of code in the compiled application.

  • Although it may be obvious, it's important to remember that the constant defined by #Const is evaluated at compile time, and therefore doesn't 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 taking advantage of the system's enhanced sound capabilities:

    If waveOutGetNumDevs > 0 Then
       #Const ccSoundEnabled = True
    Endif
    ...
    #If ccSoundEnabled Then
       ' Include code for sound-enabled systems
    #Else
       ' Include code for systems without a sound card
    #End If

    However, the code doesn't work as expected, since it includes or excludes the code supporting a sound card based on the state of the development machine, rather than the machine on which the application is running.

See Also

#If...Then...#Else Directive
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.175