Constants Defined Through the Vbvba 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:

• 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

116 Chapter 7- The Language Reference

#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

Was this article helpful?

0 0

Post a comment