Q:

Using Preprocessor Directives in Visual Studio 2010 with C#

I have a C# application that I am building with Visual Studio 2010. To help me with some of my routine tasks in the app, I wanted to set some values if I compiled the app in debug mode. Naturally, I though preprocessor directives would be a good idea. My problem is, I don't quite understand how to use them. At this time, I have a block of code that looks like this:
#define DEBUG

... // Other code in my app

#if DEBUG
  myVariable = debugValue;
#endif

My problem is, when I compile my app in release mode, myVariable still gets set to debugValue. It's like I'm not properly defining my preprocessor variable or I'm not configuring my compiler correctly. Can anybody explain to me what I need to do so that myVariable is only set to debugValue when I compile the app in debug mode?
Thank you!

A:

If you are using #define DEBUG to specify the debug symbol, switching to release mode will still provide the symbol, since you are explicitly defining it.
Try removing the #define DEBUG line in your code files.  By default VS defines DEBUG and TRACE in debug mode and TRACE in release mode, so its not necessary to explicitly define them.

A:

If you aren't aware of it, you might want to check out the "Conditional" attribute.  This lets you decorate a method rather than inlining preprocessor directives:
class SomeClass 
{
   public void ProductionOperation()
   {
      //Doin' production stuff
       Log(someProductionVariable);
   }

   [Conditional("DEBUG")]
   public static void Log(string message) 
   {
       //Write to a file
   }
}

If you're compiling in debug mode, the log method will write to a file.  If you're compiling in release mode, the conditional method becomes a no-op.  Only thing to bear in mind here is that the conditional code will make it into your assembly, unlike if you preempt it with the preprocessor - this is a runtime distinction.  However, as long as you don't mind that, I've found this keeps the code cleaner.  
(If you're going to do this, you don't want to be #defining or #undefining the DEBUG variable anywhere in your code).

A:

The DEBUG constant is actually defined in your project properties.  Go to Project Properties->Build tab->Define DEBUG constant.
By explicitly declaring that constant, you are overriding the one declared by VS.

