Results 1 to 13 of 13

Thread: [RESOLVED] Check If In IDE or Not [.NET 2005]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Resolved [RESOLVED] Check If In IDE or Not [.NET 2005]

    I did a search and found this thread...

    http://www.vbforums.com/showthread.p...ghlight=envdte

    I tried the examples there to figure out the mode of the application...

    Running it from IDE it returns "Debug"...

    Running it in exe form from bin or release folder It also returns "Debug".

    To be honest I don't even know what exe to use anyways. So maybe thats why it's not working. From my application folder I see a billion exes and was confused as to which one to use? Anyways assuming that i ran the right Exe.. What should the mode of the debugger say? "Release"?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If In IDE or Not [.NET 2005]

    What exactly is it that you want to do and under what circumstances? There are only two executables that should be being run: the one from bin\Debug when you're debugging and the one from bin\Release when you release your product.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Re: Check If In IDE or Not [.NET 2005]

    Well I run certain code in the exe that I don't want to have run in the ide.. so I need to figure out if the IDE exists or not.

    Right now i just exclude it in the ide and then add it when I build it.. but that can get annoying... so i need a way to figure out if the IDE exists or not.

    in vb6 I used..

    VB Code:
    1. Private Function InIDE() As Boolean
    2.     On Error GoTo errhand
    3.     InIDE = False
    4.     Debug.Print 1 / 0
    5.     Exit Function
    6. errhand:
    7.     InIDE = True
    8. End Function

    which I got from the forums..

    So can something similar to that be done in .net 05?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If In IDE or Not [.NET 2005]

    Firstly, don't use that form of error handling in .NET apps. Learn how to use structured exception handling with the Try...Catch...Finally...End Try structure.

    Secondly, what I think you actually mean is that you want to execute different code depending on whether this is a debug or release build. In that case you use conditional compilation. Your code doesn't have to check for anything because the compiler does the work and only compiles the appropriate code for the current configuration:
    VB Code:
    1. #If DEBUG Then
    2.         'Place code to execute while debugging here.
    3. #Else
    4.         'Place code to execute in final here.
    5. #End If
    You can two complete alternatives or you can just add something extra for one configuration, e.g.
    VB Code:
    1. Try
    2.             'Do something that might throw an exception here.
    3.         Catch ex As Exception
    4. #If DEBUG Then
    5.             MessageBox.Show(ex.ToString())
    6. #End If
    7.             MessageBox.Show("An error occurred while doing something.")
    8.         End Try
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If In IDE or Not [.NET 2005]

    Having said that, you can make calls to the methods of the Debug class with impunity because they'll just be essentially ignored in release code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Re: Check If In IDE or Not [.NET 2005]

    Thats what I meant, glad you understood me.

    for what I need to do this works fine.. unless you disagree?

    VB Code:
    1. Private Function InIDE() As Boolean
    2.  
    3. #If DEBUG Then
    4.         Return True
    5. #Else
    6.         Return False
    7. #End If
    8.  
    9. End Function

    Thank you very much sir.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If In IDE or Not [.NET 2005]

    No, that's not what I meant. You don't need to declare a function that tells you whether you're debugging or not. You're supposed to use conditional compilation wherever you need to do different things depending on the configuration. You don't do this:
    VB Code:
    1. Private Sub SomeMethod()
    2.     If InIDE Then
    3.         'Do one thing.
    4.     Else
    5.         'Do another.
    6.     End If
    7. End Sub
    8.  
    9. Private Function InIDE() As Boolean
    10.  
    11. #If DEBUG Then
    12.         Return True
    13. #Else
    14.         Return False
    15. #End If
    16.  
    17. End Function
    You do this:
    VB Code:
    1. Private Sub SomeMethod()
    2. #If DEBUG Then
    3.         'Do one thing.
    4. #Else
    5.         'Do another.
    6. #End If
    7. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Re: Check If In IDE or Not [.NET 2005]

    Except after the Debug Check I run code that needs to be run weather or not I'm in debug.

    Code:
    code to run in exe
    
    code to skip in debug [but i need to run in exe]
    
    code to run in exe
    I suppose I didn't phrase that right to begin with... the way you put it wouldn't work since you said it'll be skipped when you run in exe form.

    Code:
    code to run in exe
    
    If Not inIDE Then
           code to skip in debug [but i need to run in exe]
    End if
    
    code to run in exe
    I think I got that right?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Re: Check If In IDE or Not [.NET 2005]

    Yea, I just re-read my original post.. I didn't specify how I was using the data right

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Re: Check If In IDE or Not [.NET 2005]

    I suppose...

    VB Code:
    1. #If Not DEBUG Then
    2.     'Code to skip in IDE but Run in Exe
    3. #End If

    Would be better..

  11. #11
    Addicted Member
    Join Date
    Dec 2006
    Location
    Georgia
    Posts
    170

    Re: Check If In IDE or Not [.NET 2005]

    If you've got line of code, "LineOfCode1", that needs to run in both debug time and runtime (exe), and LineOfCode2 that only needs to be run in debug mode, you could just simply copy and paste LineOfCode1 to both sections.

    VB Code:
    1. Private Sub SomeMethod()
    2. #If DEBUG Then
    3.         'Do one thing.
    4.         LineOfCode1
    5.         LineOfCode2
    6. #Else
    7.         'Do another.
    8.         LineOfCode1
    9. #End If
    10. End Sub

    The compiler will strip your code of anything between "#If DEBUG Then" and "#Else", including the conditional syntax so that all your program is left with is the stuff you want to run outside of debug mode.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check If In IDE or Not [.NET 2005]

    There's no point writing LineOfCode1 twice.
    VB Code:
    1. Private Sub SomeMethod()
    2.         LineOfCode1
    3. #If DEBUG Then
    4.         LineOfCode2
    5. #End If
    6.     End Sub
    is the most correct way to write that code.
    Last edited by jmcilhinney; Feb 12th, 2007 at 09:27 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Posts
    64

    Re: Check If In IDE or Not [.NET 2005]

    Thanks jmcilhinney for helping me figure that out..

    Problem Resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width