Hi, I am having problems with my IF statements.

Basically, what I have done is set random numbers to 2 labels. (Random numbers are 1 or 0)

What I am trying to make the IF statement do, is see that the label is holding one of those random numbers, and then if it is "1" a calculation must be made, have a look at the code please:

if (this.LblANDa.Text = "1") //here is the syntax error.. cannot convert string to bool
            {
                this.ResultAND.Text = "Correct!";
            }

regards

Recommended Answers

All 6 Replies

if (Convert.ToInt(this.LblANDa.Text) == 1)
{
//your code
}

Add another "=" to your condition.

one = sign is assignment, you can't do that in the if () condition.
two = signs is used to compare.

You should use:

if (this.LblANDa.Text == "1")

instead.

:)

commented: thank you +1

Thank you that worked!

I am still trying to make my logic work though, because it is still not sending to the label the writing "Correct!" when it has identified the inputs as 1 etc, please have a look at the code:

private void SubmitBtn1_Click(object sender, EventArgs e)
        {
            if (this.LblANDa.Text == "1" && this.LblANDb.Text == "1" && this.textBox1.Text == "1")
            {
                this.ResultAND.Text = "Correct!";
            }
            
            if (this.LblANDa.Text == "0" && this.LblANDb.Text == "0" && this.textBox1.Text == "0"
                
                || this.LblANDa.Text == "0" && this.LblANDb.Text == "0" && this.textBox1.Text == "1"

                || this.LblANDa.Text == "0" && this.LblANDb.Text == "1" && this.textBox1.Text == "0"

                || this.LblANDa.Text == "0" && this.LblANDb.Text == "1" && this.textBox1.Text == "1"

                || this.LblANDa.Text == "1" && this.LblANDb.Text == "0" && this.textBox1.Text == "0"

                || this.LblANDa.Text == "1" && this.LblANDb.Text == "0" && this.textBox1.Text == "1") 
            {
                this.ResultAND.Text = "Incorrect!";
            }
        }

What i am saying at the first if statement, is, if both labels are "1" aswell as the input from the user into the textbox is "1", then the label (this.ResultAND) must say correct!

Nothing is happening on my Form at the moment!

Regards

Okay nevermind, I have figured out the problem! thank youuu

Hmm...that's a big if statement.

Have you considered dropping the second if statement and just using an else one? If the first one is not true, it uses the code in the else statement. Just a thought.

If it's not writing "Correct!" does it write "Incorrect!" or does it write nothing at all?

Yes, thank you for that,

Here is the code i have now:

// AND Gate Submit Btn
        private void SubmitBtn1_Click_1(object sender, EventArgs e)
        { // These are all of the variations where the user is correct..
            if (this.LblANDa.Text == "1" && this.LblANDb.Text == "1" && this.textBox1.Text == "1" ||
                this.LblANDa.Text == "0" && this.LblANDb.Text == "0" && this.textBox1.Text == "0" ||
                this.LblANDa.Text == "0" && this.LblANDb.Text == "1" && this.textBox1.Text == "0" ||
                this.LblANDa.Text == "1" && this.LblANDb.Text == "0" && this.textBox1.Text == "0")
            {
                this.ResultAND.Text = "Correct!";
            }
            // Anything else, represent the following..
            else                
            {
                this.ResultAND.Text = "Incorrect!";
            }
        }

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.