Convert Int to bool

Asked

Viewed 1,005 times

3

Guys, I need some help from you inserir a descrição da imagem aqui

    if (usr.Excluido = 0)
    {
        ModelState.AddModelError("", "Usuário bloqueado.");
        return View(model);
    }

The deleted variable is as bool on my system, I am doing a login check if Excluded = 0 the user has access to the system if it is Excluded = 1 it falls inside this if

if (usr.Excluido = 0)
            {
                ModelState.AddModelError("", "Usuário bloqueado.");
                return View(model);
            }

inserir a descrição da imagem aqui

4 answers

1

Cannot compare integers to booleans.

An important addendum: If in your database you see 1 or 0, I’m guessing you’re using SQL Server, and, boolean in SQL Server is set to Bit (true or false; 1 or 0) - And that’s not necessarily an integer.

In the image that posted is clear the understanding.

Then your case the comparison should be made as follows:

if (usr.Excluido == true)
{
    //Sua implementação aqui...
}

Or...

if (usr.Excluido)
{
    //Sua implementação aqui...
}

But from what I understand, you wish to compare usr.Excluido is a boolano object and usuarioLogado.Excluido is the flag that comes from your bank, but is of the whole type.

If that’s what I understand, then I’d be more or less like this:

if (usr.Excluido == Convert.ToBoolean(usuarioLogado.Excluido))
{
    //Sua implementação aqui...
}

I hope I’ve helped.

0

Then the comparison should be made with two equal (==), so your code will look like this:

if (usr.Excluido == 0)
{
    ModelState.AddModelError("", "Usuário bloqueado.");
    return View(model);
}

Example: http://rextester.com/RLBL96990

  • I had already done it and gives the same mistake

  • posted another photo on the theme

  • It should work as an example: http://rextester.com/RLBL96990

  • Yes, but my variable is either bool or true or false, only I’m not able to do the validation with true or false

  • So just use true false, no need for numeric comparison

  • @Reno responded the correct way for your accomplishment

  • So the problem is that with true or false I am not entering if that would be Modelstate.Addmodelerror("", "User locked.");

  • It doesn’t make sense, I’d have to go in

Show 4 more comments

0

In C#, you cannot compare integers with booleans. Its property is boolean, so you should compare it only with values true or false.

In your case, the comparison should be made as follows::

if (usr.Excluido == true)
{
    ModelState.AddModelError("", "Usuário bloqueado.");
    return View(model);
}

If you want, you can also omit the comparison, taking into account the value of the property itself to execute the if:

if (usr.Excluido)
{
    ModelState.AddModelError("", "Usuário bloqueado.");
    return View(model);
}
  • So @Brunocosta I did the test type, I have two users in my bank one with deleted = 0 and one deleted = 1

  • then make a comparison of the usr.Excluido with the property coming from your bank, for example: if (usr.Excluido && usuarioBanco.Excluido == 1)

0

I don’t see why compare bool with int.
The correct way to do what you want (from what I understand) would be:

if (usr.Excluido)
{
    ModelState.AddModelError("", "Usuário bloqueado.");
    return View(model);
}

There’s no reason why the code above should go wrong. But if you really want to convert int to bool you can use the following code:

bool fromInt(int i)
{
    if (i == 0)
        return false;
    return true;
}
...
if (usr.Excluido == fromInt(1))
{
   ModelState.AddModelError("", "Usuário bloqueado.");
   return View(model);
}

Note: You should use two equal == for comparison, and not just one (=).

Browser other questions tagged

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