Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#If DEBUG is ignored (VB.net or C#)

Tags:

c#

vb.net

I have several of these in my code which have been working fine so far:

#If DEBUG Then    
   ... some code here       
#End If     

Now, i am noticing that, lately, the code inside the " #If DEBUG Then ... #End If" gets execute also in "Release Mode".

This is strange and did not happen before. What could have happened so that the #If DEBUG are now being ignored (they are ignored both in debug in the IDE or the final executable) ?

I have applied Clean, Rebuild, etc.: no luck. Thank you for any hints and help.

-Pam

like image 431
Pam Avatar asked Aug 02 '11 12:08

Pam


1 Answers

Firstly, make sure you understand the difference between how you're running the code and how you're building it. Too many people equate "launching in a debugger" with "the debug version" and "launching not in a debugger" with "the release version". They're completely orthogonal - you can launch a release build in a debugger (typically with less information available) and you can launch a debug build not in a debugger. Apologies if you were already aware of this.

Now, assuming you really have changed the project configuration you're building to Release, you need to check the project properties for that specific configuration. I don't know what it looks like in VB, but in C# in the project properties, in the build tab, there will be a list of defined symbols - that is what affects whether #if DEBUG code is built or not. Perhaps someone has copied over the project configuration from Debug into Release?

EDIT: One way to check this at build time is:

#if DEBUG
#error This shouldn't happen
#endif

In a release build, that should build without error. In debug, it won't.

EDIT: Another option is that your overall solution configuration is now referring to the wrong project configuration types. I can't remember the exact menu name, but if you look around Project for Configuration Manager, you should be able to bring up a grid mapping "Project" and "Solution Configuration" to the project configuration to build.

like image 121
Jon Skeet Avatar answered Oct 20 '22 23:10

Jon Skeet