How to turn a bool into int?

Asked

Viewed 419 times

5

I’m trying to do the following: if the guess value (_TextValPalite) is enabled then it will check that this number is not different from a minimum value and a maximum value.

Ex: between 1 and 10 I can write 5, but not 11.

if(_TextValPalite.Enabled)
        {
            if (Convert.ToInt32(_TextValMin.Text) & Convert.ToInt32(_TextValMax.Text) != Convert.ToInt32(_TextValPalite.Text))
            {
                MessageBox.Show("Você não pode fazer isso");
                return;
            }
        }

But it returns this error:

Operator '&' cannot be applied to operands of type 'int' and 'bool'

I can do + - * / operations normally, but how to do the bool interpret this? turning it into a int? how could I do that?

  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site.

3 answers

4

You cannot understand the errors literally, in most bvezes the problem is another: you are using the wrong operator, the correct one is the && which is the AND logical. The & is the AND bit multiplier, which is not what you want there. Also missing a part of the comparison, have to compare with the guess the two operations.

The compiler was "lost" because he had an integer in the first part of the operator, and a boolean in the second, with the wrong operator, he gave this error message that he got under certain rules. If they were both boolean, he’d have better information to give. A comparison (which uses a relational operator, like the different one) always gives a boolean, missing this operation in the first subexpression.

if (Convert.ToInt32(_TextValMin.Text) != Convert.ToInt32(_TextValPalite.Text) && Convert.ToInt32(_TextValMax.Text) != Convert.ToInt32(_TextValPalite.Text))

I put in the Github for future reference.

Although this code has another error already defined in previous question.

If you type something wrong the application will break.

  • Both & & and & return the same error.

  • I had forgotten to put the rest of the comparison.

3

First you must use two signs && for the instruction and, and you should also do a check for each item, see:

(Convert.ToInt32(_TextValMin.Text) != Convert.ToInt32(_TextValPalite.Text)) &&
(Convert.ToInt32(_TextValMax.Text) != Convert.ToInt32(_TextValPalite.Text))

Even so, you still won’t get the result you expect, you have to buy if the values are between the minimum and maximum, just different as you did, could be any value, for example:

valorMinimo = 1;
valorMaximo = 10;
valor = 11;

Doing a table test for this case with your example:

valor != valorMinimo ? True
valor != valorMaximo ? True
Resultado: True

Now, what you really need:

valor >= valorMinimo ? True
valor <= valorMaximo ? False
Resultado: False

See the example in code:

var valorMinimo = Convert.ToInt32(_TextValMin.Text);
var valorMaximo = Convert.ToInt32(_TextValMax.Text);
var valor = Convert.ToInt32(_TextValPalite.Text);
var estaEntreIntervalo = (valor <= valorMaximo) && (valor >= valorMinimo);
if (!estaEntreIntervalo) { //Se não esta entre o intervalo
    MessageBox.Show("Você não pode fazer isso");
    return;
}

1

if(_TextValPalite.Enabled)
        {
            if (Convert.ToInt32(_TextValMin.Text) && Convert.ToInt32(_TextValMax.Text) != Convert.ToInt32(_TextValPalite.Text))
            {
                MessageBox.Show("Você não pode fazer isso");
                return;
            }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.