Can anyone explain to me why this mistake?

Asked

Viewed 49 times

3

Code:

   int jogo = 0;

   public void ganhou()
   {
      MessageBox.Show("O jogador " + jogador_atual + " ganhou!");
      reniciar();
      jogo++;
      lb_numero_jogo.Text = Convert.ToInt32(jogo);
   }

Error: Cannot implicitly Convert type 'int' to 'string'.

iPrint do erro

  • First, what type of variable is "GAME" int? For what the error says is "It is not possible to make an implicit conversion from "int" to "string"

1 answer

6


The C# is heavily typed, that is, when assigning a value to a string, for example, the value should also be a string.

Try it this way:

MessageBox.Show("O jogador " + jogador_atual + " ganhou!");
reniciar();
jogo++;
lb_numero_jogo.Text = jogo.ToString();
  • You can explain better why it works and why it doesn’t. My program had 3 string if I’m not mistaken but in this case it did not involve any string. Thanks for the attention and help

  • You are assigning a value to the "lb_numero.jogo.Text" property, which is a String. So, what you assign to it must also be a String. By default, every variable or object in C# has a ". Tostring()" method, I just made use of it.

Browser other questions tagged

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