Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] Debug only code?

  1. #1

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Resolved [RESOLVED] [2.0] Debug only code?

    Hey everyone.

    I've got a few segments of code (usually stopwatch objects) I want to run only during debug. When the program is built for release, these lines would merely slow the program down. Is there a way to manage this in C# express?
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

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

    Re: [2.0] Debug only code?

    CSharp Code:
    1. #if DEBUG
    2.             // Code here will only be compiled into a Debug build.
    3. #else
    4.             // Code here will only be compiled into a Release build.
    5. #endif
    Note that the extra code is NOT executable C# code. It is a directive to the preprocessor so the compiler never even sees it. As such it can go anywhere you like. It can even be used in ways that may look like they'd create compilation errors but, because some of the code is never compiled they don't, e.g.
    CSharp Code:
    1. #if DEBUG
    2.         public void SomeMethod(string someValue)
    3.         {
    4.             MessageBox.Show(someValue);
    5. #else
    6.         public void SomeMethod()
    7.         {
    8. #endif
    9.             // Do something here.
    10.         }
    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
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: [2.0] Debug only code?

    Awsome! I'd used preprocessor code before, but couldn't seem to track down the right combination of it to get debug only stuff working.

    T'anks!
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

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