Running within IDE or Compiled App

First off, I'm developing in VB.Net 2010 Pro

What I'm attempting to do is read from one database when I'm running within the IDE and another database when the app is running as a compiled app.

This is what I came up with.  I'd like to know if this works and/or if there's a better way?

To start, I've added DebugArg to the project properties Debug Tab in the Start Options/Command line arguments.

Then I have this code, which runs at the start of the app.  When compiled, the app itself won't have any arguments.


Const m_sConnect_App As String = "Data Source=0.0.0.40;Initial Catalog=DB_Production;User Id=<UserName>;Password=<UserPassword>;"
Const m_sConnect_Debug As String = "Data Source=0.0.0.40;Initial Catalog=DB_Debug;User Id=<UserName>;Password=<UserPassword>;"
Private m_sOPISConnect = ""

Sub Main()

    If My.Application.CommandLineArgs.Count > 0 Then
        m_sConnect = m_sConnect_Debug
    Else
        m_sConnect = m_sConnect_App
    End If

'
'
'
End Sub

Open in new window


TIA
LVL 10
ClifAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Clif;

You can use the preprocess directives to accomplish what you need. When your project is in debug configuration mode it defines a variable called DEBUG all in capital letters which will allow you to selectively compile certain statements and not compile other statements. So the #if statement below will compile the first m_sConnect statement when in release and the second when in debug mode. This will allow you to use the same variable name in code and the correct version will be assigned to it.

#If Not Debug Then
    Const m_sConnect As String = "Data Source=0.0.0.40;Initial Catalog=DB_Production;User Id=<UserName>;Password=<UserPassword>;"
#Else
    Const m_sConnect As String = "Data Source=0.0.0.40;Initial Catalog=DB_Debug;User Id=<UserName>;Password=<UserPassword>;"
#End If
Private m_sOPISConnect = ""

Sub Main()

    Console.WriteLine( m_sConnect )

'
'
'
End Sub

Open in new window

0
Fernando SotoRetiredCommented:
Here is the documentation for Directives (Visual Basic).
0
ClifAuthor Commented:
Thank for the info, but there's a small problem with it.

The directive works off the "Solution Configuration".  In other words, if I'm developing and have set the solution configuration to "Release", it will run the non-debug code in the IDE.  

This is not quite what I want.  Occasionally I need to run within the IDE with the configuration set to "Release" (to test speed, etc).

Ultimately, I need to know only when the project is running in the IDE or as a compiled app.
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Robert SchuttSoftware EngineerCommented:
Just last week I was looking for that very functionality and found:
If System.Diagnostics.Debugger.IsAttached Then ' running in Visual Studio
    ' ...
End If

Open in new window

Note, it's not 100% airtight (as I would think another process could attach to yours as debugger, not only Visual Studio), but it did the trick for me...
0
Fernando SotoRetiredCommented:
Hi Clif;

Please read the link I posted to the documentation. You will find that you can create your own #Directive constants and use them. In the code snippet below I have create a  directive constant called Testing and used it instead of using the environment debug constant.

#Const Testing = False

#If Not Testing Then
    Const m_sConnect As String = "Data Source=0.0.0.40;Initial Catalog=DB_Production;User Id=<UserName>;Password=<UserPassword>;"
#Else
    Const m_sConnect As String = "Data Source=0.0.0.40;Initial Catalog=DB_Debug;User Id=<UserName>;Password=<UserPassword>;"
#End If

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Console.WriteLine(m_sConnect)

    End Sub

Open in new window

0
ClifAuthor Commented:
FernandoSoto,

I understand what you're doing with creating directives.  However that still forces me to remember to change the assignment of the directive from False to True when I create a build, which is my problem to begin with.  I keep forgetting to change the "Initial Catalog" when I create a build.

Also, are y'all suggesting that my idea posted in the OP won't work?
0
Robert SchuttSoftware EngineerCommented:
Your solution with the argument should work as well but is not as clean as checking for debugger. For example, it could be possible for a user to add an argument to a shortcut to your exe and access your debug database. I don't know why they would want to, but still... Also, in your project, you may need to add that dummy argument in different builds and may forget when you ever create a new platform (x64, Itanium) and also if you ever want to add other argument processing to your exe, you could have a new problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Well then you could always use a command line parameter to do what you need but there again you will need to remember to start the application with the command line parameter when you need it.
0
ClifAuthor Commented:
Thanks,  I'm in full control of the installation, so I know that no one will be adding a command line parameter.  

If they do, then it's user error, not app error.  :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.