How to transform a bool into int?

4

I'm trying to do the following: if the guess value ( _TextValPalite ) is enabled then it will check if that 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 do bool interpret this? turning it into a int ? how could I do that?

    
asked by anonymous 20.05.2016 / 14:45

3 answers

4

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

The compiler was lost because it had an integer in the first part of the operator, and a boolean in the second, with the wrong operator, it gave this error message. If both were booleans, he would have better information to give. A comparison (which uses a relational operator, such as the one different) always gives a boolean, this operation was missing in the first subexpression.

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

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

    
20.05.2016 / 14:48
2

You should first use two && signals for the and statement, 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 though, you still will not get the result you expect, you have to buy if the values are between the minimum and maximum, just different as you did, it 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;
}
    
20.05.2016 / 14:50
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;
            }
        }
    
20.05.2016 / 14:55