Justin Dearing
Justin Dearing

Reputation: 14948

What is the difference between #const and const?

This class, which runs just fine contains lines like #Const UseScriptingDictionaryIfAvailable = True. I normally use Const UseScriptingDictionaryIfAvailable As Boolean = True.

I've noticed you can't explicitly declare the type with #const. Whereas As Boolean is optional in the later, its forbidden in the former. Is that the only difference? Is there an internal difference?

Upvotes: 3

Views: 850

Answers (2)

zedfoxus
zedfoxus

Reputation: 37089

That # is for conditional compilation. If you want to conditionally compile a certain piece of code, you can use #. For example:

#Const MyEnv = "Testing"

Sub TestMacro()

 #If MyEnv = "Testing" Then
   ' do something here
   Debug.Print "Logging for testing"
   Dim X as String
   X = "..."
 #Else
   Dim Y as Int
   Y = 100
 #End If

End Sub

https://usefulgyaan.wordpress.com/2013/06/26/vba-trick-of-the-week-conditional-compiling/ link provides a good description of conditional compilation.

http://www.utteraccess.com/wiki/index.php/Conditional_Compilation also provides some good inputs.

Upvotes: 4

Mathieu Guindon
Mathieu Guindon

Reputation: 71187

Const is your everyday "normal" constant declaration.

#Const is different, it lets you define constants specifically for use with compiler directives like #If.

A "normal" constant wouldn't be available for the compiler to use for conditional compilation:

Const DEBUG As Boolean = True

However this would:

#Const DEBUG = True

Upvotes: 0

Related Questions