Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>An instance variable would probably be the way to do what you want. You could make it static to persist the same value for the life of the program (or thread depending on your static memory model), or make it an ordinary instance var to control it over the life of an object instance. If that instance is a singleton, they'll behave the same way.</p> <pre><code>#if DEBUG private /*static*/ bool s_bDoDebugOnlyCode = false; #endif void foo() { // ... #if DEBUG if (s_bDoDebugOnlyCode) { // Code here gets executed only when compiled with the DEBUG constant, // and when the person debugging manually sets the bool above to true. // It then stays for the rest of the session until they set it to false. } #endif // ... } </code></pre> <p>Just to be complete, pragmas (preprocessor directives) are considered a bit of a kludge to use to control program flow. .NET has a built-in answer for half of this problem, using the "Conditional" attribute.</p> <pre><code>private /*static*/ bool doDebugOnlyCode = false; [Conditional("DEBUG")] void foo() { // ... if (doDebugOnlyCode) { // Code here gets executed only when compiled with the DEBUG constant, // and when the person debugging manually sets the bool above to true. // It then stays for the rest of the session until they set it to false. } // ... } </code></pre> <p>No pragmas, much cleaner. The downside is that Conditional can only be applied to methods, so you'll have to deal with a boolean variable that doesn't do anything in a release build. As the variable exists solely to be toggled from the VS execution host, and in a release build its value doesn't matter, it's pretty harmless.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload