views:

121

answers:

5

I'm writing an application wherein I have some debug code that I do not wish to delete, but I wish it to be modified or removed when compiling for release/publish. For example, I would like something like this in a debug build:

MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

...to become this in a release build:

MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

Ideally, I was hoping to do something like this:

#if DEBUG_BUILD
   MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif

I would prefer to not have to add/remove a Conditional Compilation Symbol in the project properties every time I change the build type; it should happen automatically. Is there a way to do this in Microsoft Visual C# 2008 Express Edition? Thanks.

+6  A: 

Use:

#if DEBUG
  // Debug work here
#else
  // Release work here
#endif

If you do that, just make sure to turn on the "Define DEBUG Constant" toggle in the property pages (Build page of the Project's properties), and it will work. This is set to true by default for new C# projects. DEBUG will get defined for you (by default) by the C# compiler.

Reed Copsey
A: 

You could write an extension method that contains the conditional so you don't need to keep duplicating it every time

public static class ExceptionExtensions
{
    public static String ToMyString(this Exception ex)
    {
#if DEBUG
        return ex.ToString();
#else
        return ex.Message;
#endif
    }
}

That would be how I'd go about it

And then your line of code would be

MessageBox.Show(ex.ToMyString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

And DEBUG should be defined already

Chad
Ow. This looks like an abuse of extension methods to me.
Robert P
@Robert P, why?
Chad
A: 

There is a class you can use to write your debug statements namespace: system.diagnostics Debug.Assert is what you want to use

http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert.aspx

Also look at the Debug class for all debugging: http://msdn.microsoft.com/en-us/library/6x31ezs1.aspx

greektreat
@greektreat: what does Debug.Assert have to do with the question?
Chad
for showing messages it's not but if he is putting code in for Debugging then Asserts could be more usefull then showing a message box. I am showing more advanced options you can use
greektreat
A: 

I do not know if the express editions have this, but Visual Studio has this already built-in for you in C#.

In your toolbar you surely have the dropdown that let's you (by default) choose between debug and release build, the debug build defines the DEBUG symbol so you can use:

#ifdef DEBUG
    // debug code
#endif
Francisco Soto
+7  A: 

You can also use this attribute.

[Conditional("DEBUG")]

This has a couple of advantages over the preprocessor directive.

All calls to methods marked conditional will be replaced with Nops if the conditional symbol isn't defined, which saves you having to modify all calls to it.

Your code will checked for errors even when the symbol is not defined. (Unlike when using #if DEBUG, which ignores the code in #else during compilation)

Mark H
That is freaking awesome.
Robert P