Pam
Pam

Reputation: 504

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

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

Upvotes: 21

Views: 37682

Answers (11)

redaxe
redaxe

Reputation: 33

And don't be a rookie like me and try adding #If DEBUG on inline code and wonder why it doesn't work - the DEBUG constant is simply not available to the inline code segment. It only works on code-behind (which makes sense since it is a compiler directive).

Upvotes: 1

Morcilla de Arroz
Morcilla de Arroz

Reputation: 2182

On NET CORE C#, it's not exactly a "check". You will need go to "project properties > Compilation > General > Custom compilation symbols". Once there, you can ADD the DEBUG var (or another of the supported on you Visual Studio enviroment).

Type the name and press Add.

Project properties vars

Upvotes: 0

Karthikeyan P
Karthikeyan P

Reputation: 1267

If you are using VB.NETFramework v4.5 then use like

If Debugger.IsAttached Then
    '... some code here
End If

Upvotes: 1

Jeroen VL
Jeroen VL

Reputation: 351

Had a similar problem where "DEBUG" was never true. Tried by doing an uncheck and check of the "Define DEBUG constant" checkbox and rebuilding everytime but that did not work.

My solution was to define "DEBUG" manually in the "Conditional compilation symbols" textbox for the Debug configuration. When rebuilding, Visual Studio 2019 automatically removed the DEBUG symbol from the textbox (because this indeed should not be there) and from then on it worked again. When i switched from Debug to Release the correct lines got greyed out. This seems to be a possible bug in VS 2019 (16.4.5)?

Upvotes: 2

Ali Mahmoodi
Ali Mahmoodi

Reputation: 1204

If you are using ASP.NET make sure about this line in Web.Config file:

<compilation debug="false" targetFramework="4.5">

So if debug="true" your project runs in DEBUG mode.

Upvotes: 0

F Snyman
F Snyman

Reputation: 509

Also remember #if DEBUG must be in uppercase. e.g. #if debug won't work.

Upvotes: 1

Virgar Poulsen
Virgar Poulsen

Reputation: 477

C# Project ( Visual Studio )

  1. go to: Project Properties -> Build(tab)
  2. Select Configuration: Release
  3. Uncheck "Define DEBUG constant"

  4. Now select Configuration: Debug

  5. Check "Define DEBUG constant"

  6. In your code, you can now type the following ( DEBUG with uppercase )

#IF DEBUG

// Debugging code goes here

#ENDIF

Upvotes: 29

Gage
Gage

Reputation: 7513

Under Project Properties / Compile / Advanced Compile Options there is a checkbox called "Define Debug Constant" that sets this.

Check out: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24658238.html

EDIT: Try this initializing with this:

#If CONFIG = "Debug" Then
#CONST DEBUG = true

#if CONFIG = "Release" Then
#CONST DEBUG = false

Upvotes: 23

Jon Skeet
Jon Skeet

Reputation: 1504062

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.

Upvotes: 36

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52157

Did you, by any chance, tick the "Define DEBUG constant" for the Release configuration, while you were in the Project Properties / Build?

Also make sure you are not building the project-level Debug configuration within the solution-level Release configuration (see the Configuration Manager).

Upvotes: 6

user195488
user195488

Reputation:

undefine DEBUG and that will not execute that portion.

Upvotes: 0

Related Questions