D B
asked on
Setting the value of a constant depending on environment
I have a VB application. I have both a production and test database set up.
In a BASIC module, I have four constants that define the parameters for a database connection (DATASOURCE, INITIAL_CATALOG, UID, PWD)
I want the values of these to be set to different values if I am running within the VB IDE than when I compile the application. Is there some way I can accomplish this?
Doug
In a BASIC module, I have four constants that define the parameters for a database connection (DATASOURCE, INITIAL_CATALOG, UID, PWD)
I want the values of these to be set to different values if I am running within the VB IDE than when I compile the application. Is there some way I can accomplish this?
Doug
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could use a compiler directive. Only risk here is that you forget to change it before the production compile. Not that I've ever done that before ;)
Otherwise you could make them variables, and set their values like this:
if InIDE then
set variables to x
else
set variables to y
endif
Use this function for InIDE:
Public Function InIDE(Optional SetMode As Boolean = False) As Boolean
'------------------------- ---------- ---------- ---------- --------
'Call this with no arguments so the optional argument is false.
'this will cause a recursive call via Debug.Assert that will set
'the static return value True. If not running in the IDE then
'Debug.Assert will not execute and the static return value will
'remain false.
'------------------------- ---------- ---------- ---------- --------
Static bInIDE As Boolean
bInIDE = SetMode
If Not bInIDE Then Debug.Assert InIDE(True)
InIDE = bInIDE
End Function
Otherwise you could make them variables, and set their values like this:
if InIDE then
set variables to x
else
set variables to y
endif
Use this function for InIDE:
Public Function InIDE(Optional SetMode As Boolean = False) As Boolean
'-------------------------
'Call this with no arguments so the optional argument is false.
'this will cause a recursive call via Debug.Assert that will set
'the static return value True. If not running in the IDE then
'Debug.Assert will not execute and the static return value will
'remain false.
'-------------------------
Static bInIDE As Boolean
bInIDE = SetMode
If Not bInIDE Then Debug.Assert InIDE(True)
InIDE = bInIDE
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hmm - dbbishop, I am glad that you have a working solution, but I am also wondering why you only split the points between Raynard7 and zorvek.
ASKER
Nightman: Sorry. I gave points to zorvek because he had the first answer. I liked (and used) the example in the link Raynard7 provided, and also that it provided a way to accomplish the same thing using an API call instead of debug.assert. Functionally, there was not a whole lot of difference between what zorvek provided and what you suggested. You can give ten programmers the same problem and get ten solutions, none of them necessarily wrong. You can tear each on apart and show how solution six would take 3.5 nanoseconds longer to run through 2,000 iterations then solution eight, but if you are only running it once through a user interface, who really cares?
I'll make it up with my renaming a SQL Server database file question :-)
Doug
I'll make it up with my renaming a SQL Server database file question :-)
Doug
No problem Doug - just interested to know why.
Enjoy your vacation and we'll see you near the end of the month.
Cheers
Night
Enjoy your vacation and we'll see you near the end of the month.
Cheers
Night
Public Function IsDebugMode() As Boolean
' This function returns True if code is running in VB debugger, False otherwise.
Dim DebugMode As Boolean
' Following line is not included in compiled code
Debug.Assert SetDebugModeTrue(DebugMode
IsDebugMode = DebugMode
End Function
Kevin