pauldbentley
pauldbentley

Reputation: 49

bool not being set to true in c#

I simply cant get my head around this simple issue.

I have a bool which I am asigning to the output of a test:

// these are passed in to the function and will vary
bool inReview = true;
char status = 'V';

bool test = (inReview && status != 'M') || !inReview;

Which evaluates to:

bool test = (true && true) || !true;

Which should be true - but the debugger shows the value of "test" to be false.

When I try this:

bool inReview = true;
char status = 'V';

bool test = false;

if ((inReivew && status != 'M') || !inReview)
{
    test = true;
}

It drops into the if and the debugger shows the value of "test" to be true.

Now here is something else very strange, if I do:

bool test = (inReview && status != 'M') || !inReview;
bool test2 = (inReview && status != 'M') || !inReview;

Stepping through with the debugger - test at first is false, test2 becomes true instantly, but then when I check test it is now true!?

Also, if I try:

bool test = (inReview && status != 'M') || !inReview;
if (test)
{
    string s = "WTF?";
}

Stepping through - test at first is false, then it does step into the if and the value is now true!?

Upvotes: 0

Views: 751

Answers (2)

Dennis Traub
Dennis Traub

Reputation: 51694

When the debugger steps into a line then that line still has to be evaluated. You must step past the line for the assignment to occur. Once the debugger is at the closing bracket (}) the variable's value should have been set.

Upvotes: 3

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241959

At what line are you evaluating in the debugger? If you simply set a breakpoint on the line where you are doing the assignment, of course the value will initially be false, because the code for that line has not yet executed. Step to the next line and you should see the correct result.

Upvotes: 0

Related Questions