Himaja
Himaja

Reputation: 129

Preprocessor in C# is not working correctly

#if(DEBUG)
    ......Code......
#else
    ......Code......
#endif

I have some code like this. If my application is running in Debug mode it should execute the #if(DEBUG) part, if it is running in Release mode it should execute the #else part. However, it is only executing the #if(DEBUG) part no matter which mode it is running in.

Am using WPF application with VS2010

Can anyone help me?

Upvotes: 10

Views: 11231

Answers (5)

Chuck Conway
Chuck Conway

Reputation: 16435

It depends on how you create your configurations. For example if you create your configuration and use debug or release as a template DEBUG or RELEASE will be copied to the defined constraints element. It will not change the defined constraints element (in the project file) to the new configuration name.

Open up the project file and look for the configuration sections. Make sure the Platform, the below example it is "PROD" has an entry in the DefineConstants element. If it does the pre-compiler directives will don't work as expected in the code.

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'PROD|x86'">
  <DefineConstants>PROD;TRACE</DefineConstants>
    <OutputPath>bin\x86\PROD\</OutputPath>
  </PropertyGroup>

Upvotes: 3

BlueMonkMN
BlueMonkMN

Reputation: 25601

Create a new project using all the default settings and check that you can make that work as expected. If so, your problem project must be "corrupted" in some way, perhaps by defining the DEBUG constant in the release configuration, or by having the debug project configuration selected for the release solution configuration.

Upvotes: 4

Binary Worrier
Binary Worrier

Reputation: 51711

For Debug Configuration, your project settings should look like

enter image description here

For Release they should look like this

enter image description here

Can you verify that this is the case, and let us know if it is?
If not, what is there for each configuration?

Upvotes: 17

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

I would guess that in your project properties, under Build you have checked off Define DEBUG constant.

Try setting configuration mode to Release and run your application again. Default for Release is that the DEBUG constant is not defined, if you haven't tampered with if of course ;)

If Define DEBUG constant is not checked, that means you have a #define DEBUG lurking somewhere.

So two things to do. Check constant in options under Release mode, and check for any manually defined constant. It should be one of those.

Upvotes: 0

Tommy Carlier
Tommy Carlier

Reputation: 8149

Why are you putting DEBUG between parentheses?

#if DEBUG
    Code
#else
    Code
#endif

Upvotes: 2

Related Questions