Setting variables at Compile Time using #Const

  • Thread starter Thread starter Michael Fitzpatrick
  • Start date Start date
M

Michael Fitzpatrick

Is it possible to set a variable with the value of a compile time constant.

Example:

#Const MODE_STR = "Mode1"

dim strMode as String = MODE_STR

Obviously this doesn't work. Is there a way to achieve the same thing???
 
Hi Michael,

Unfortunately, you can't do exactly what you want. #Const constants can
only be used in #If directives. For example:

#Const TEST_CONST

some code

# If TEST_CONST = "TEST" Then

some conditional code

# End If

You can do close to what you want in one of two ways. First something like

#Const MODE_STR = "Mode1"


# If MODE_STR = "Mode1" Then

Private Const MODE_STR = "Mode1"
# End If

Dim strMode As String = MODE_STR

It's probably simpler, however, to just use Const.

Private Const MODE_STR = "Mode1"

Hope this helps,
Craig VB.Net Team
 
Back
Top