if (false) { behave nice; } else { freak out; }

Using “false” in a conditional causes all variables in the else clause to return nothing while debugging. Huh? Yep. Here’s a screenshot of inside an else clause:

the name does not exist in the current context

The “comparisons” variable is declared outside the conditional and shows up as does “comparisonIndex” which is also defined outside the conditional. However, the “singleComparison” shows “The name “singleComparison” does not exist in the current context.” because it is defined inside else clause.

Stick this in Visual Studio:

if (false)
{
    // nothing here
}
else
{
    string abc = "you can't see me";
    int xyz = 123;
}

Next, set a breakpoint on the int xyz line and start debugging. When you hover your mouse over abc or xyz you’ll get nothing. You can also add watches for them but you’ll get:

The name ‘variablename’ does not exist in the current context.

I also saw this:

Unable to evaluate the expression. An outgoing call cannot be made since the application is dispatching an input-synchronous call.

The workaround is to use an expression that will always evaluate as false like this:

int fake = 0;
if (fake == 1)
{
    // nothing here
}
else
{
    string abc = "you can't see me";
    int xyz = 123;
}

That will give you the result that you actually want.

I asked about it here and that’s how I figured it out. mwb100 had found something similar for C++, and as it turns out, it was bang on. The article has a work around listed, however, it’s bad. The fix I have above is much better.

Anyways, I hope that helps someone out.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.