6

Possible Duplicate:
C# if/then directives for debug vs release

I am working on a C# project and I like to know how to add special cases while I am running the program in the debugger in the Debug Mode I have access to certain resources that I would not normally have in the release version.

Here is what keeps happening, I have admin tools built into the program, that are just for debugging, like a button that says test and put what ever code I want to into it. What keeps happening I forget to hide that button and I release it to the clients. I would love to have that test button there only while it's running in the debug mode but not any other time.

This turns on my admin tools

rpg_Admin.Visible = true;

This turns off my admin tools

rpg_Admin.Visible = false;

Is there a simple way to do this?

if Debug Mode
    rpg_Admin.Visible = true

or maybe while it's running in visual studio it's

rpg_Admin.Visible = true

but when it's running on it's own

rpg_Admin.Visible = false

I am running on Visual Studio 2010

Thanks.

Community
  • 1
  • 1
David Eaton
  • 564
  • 2
  • 10
  • 22

2 Answers2

7

Using your example, add some #if / #else / #endif directives like so:

#if DEBUG
    rpg_Admin.Visible = true;
#else
    rpg_Admin.Visible = false;
#endif

You could also apply System.Diagnostics.ConditionalAttribute attributes to any code that's only used in the debug version. That will completely remove any unnecessary code from the release build. Example:

[Conditional("DEBUG")]
public static void MyDebugOnlyMethod()
    {
    }
Tim Long
  • 13,508
  • 19
  • 79
  • 147
1

wrap the debug code in #ifdef DEBUG/#endif:

#ifdef DEBUG

// debug only code here

#endif
Z .
  • 12,657
  • 1
  • 31
  • 56
  • hum, for some reason #ifdef is not well liked, it gives me tons of errors. Is there a special place for this, I am calling right after InitializeComponent(); in my form. – David Eaton Dec 27 '12 at 15:30
  • paste your code. you should be able to use ifdef anywhere... – Z . Dec 27 '12 at 16:00