• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

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
0
dbbishop
Asked:
dbbishop
2 Solutions
 
zorvek (Kevin Jones)ConsultantCommented:
Use this function:

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
0
 
zorvek (Kevin Jones)ConsultantCommented:
Here is the other function:

Private Function SetDebugModeTrue( _
      ByRef DebugMode As Boolean _
   ) As Boolean

   ' Return True for function result to prevent assert from halting execution
   DebugMode = True
   SetDebugModeTrue = True

End Function

Kevin
0
 
NightmanCTOCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Raynard7Commented:
Hi,

The fact these are constants means that you would not be able to dynamically change them because of their very nature.

You could have global variables that are accessible from your application - and in your main module change their value dpeending if in the IDE or not.  The problem with this approach is that it means that anything else in your application could change them .

Alternativley - you could call a function when initilising your program to set a global value from true to false depending if in an ide or not - then whenever you want your value create some functions to return the value and if the global value (in ide) is true then return one value otherwise return the other.

If you want to see if you are in ide or not you can use the function located here: http://vbnet.mvps.org/index.html?code/helpers/isinide.htm
0
 
NightmanCTOCommented:
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.
0
 
dbbishopAuthor Commented:
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
0
 
NightmanCTOCommented:
No problem Doug - just interested to know why.

Enjoy your vacation and we'll see you near the end of the month.

Cheers
Night
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now