Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (bool232 = true)
            {
                if (gameTime.TotalGameTime.TotalSeconds >= 3)
                {
                    this.Exit();
                }
            }
            else
            {
            }

For some reason this code is making my game exit after 3 seconds despite "bool232" being false, the only time bool232 is true is when the game over screen is shown
any help?
Posted

Don't you mean:
C#
if (bool232 == true)

or even better:
C#
if (bool232)


(A better name for that boolean wouldn't hurt, either.)
 
Share this answer
 
Comments
RaisKazi 10-Dec-13 16:07pm    
My 5!
Ron Beyer 10-Dec-13 16:08pm    
+5, I've had many a head-scratching session when I started programming about missing that other equals sign. The single equals actually makes bool232 true, then the if evaluates it, so it will always be true in the original code.
Matt T Heffron 10-Dec-13 16:13pm    
One of the tricks I recall from my c++ days is always to put the constant first. Only a very dumb compiler will allow: if (true = variable)... It'll tell you right away what you've done wrong.
Of course, for boolean values, just use it. There's no need to compare with true.
It should read like this:
if (bool232 == true)
{
    if (gameTime.TotalGameTime.TotalSeconds >= 3)
    {
        this.Exit();
    }
}
else
{
}

You are assigning a value inside the if statement instead of comparing. The code should not even compile.
 
Share this answer
 
Comments
Ron Beyer 10-Dec-13 16:10pm    
Actually it will compile, with a warning (a "are you sure you meant to do that" one). If the person ignores warnings, then it will happily run.
Richard C Bishop 10-Dec-13 16:20pm    
Ahhh, thank you for the clarification.
CHill60 10-Dec-13 16:46pm    
Could see where you were coming from though ... if it had been (e.g.) an int then you *would* get the error "Cannot implicitly convert type 'int' to 'bool'"
Richard C Bishop 10-Dec-13 17:00pm    
Indeed, I guess I just remember compiler errors when I attempted this before.
As the others pointed out you are assigning true to your variable instead of comparing it.

I also agree that

if (bool232)


is better than

if (bool232 == true)


though if you are going to use the == a good practice (sometimes called yoda code) is to always put the constant first

if (true == bool232)
that way you will always get a compile error if you forget an '=' sign.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900