Solved

Setting the value of a constant depending on environment

Posted on 2006-11-14
7
145 Views
Last Modified: 2010-04-30
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
Comment
Question by:dbbishop
7 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17944880
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
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 50 total points
ID: 17944886
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17944890
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 35

Accepted Solution

by:
Raynard7 earned 200 total points
ID: 17944895
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17947817
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
 
LVL 15

Author Comment

by:dbbishop
ID: 17949457
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17949930
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now