Share via


Cannot implicitly convert type 'int' to 'string'

Question

Friday, September 25, 2020 9:24 PM

Hey, so basically I have to do a very simple program, but im blocked, I've tried multiples possibilities and can't seem to find the problem.

The code is fairly simple ;

public class ControleurInterface : MonoBehaviour
{
    public InputField saisieEssai;
    public int nombreMystere;
    public int nombreEssais;
    public Text textNbEssais;
    public Text textReponse;
   

    // Start is called before the first frame update
    void Start()
    {
        nombreMystere = UnityEngine.Random.Range(1, 100);
        nombreEssais = 0;
        textNbEssais.text = "Nombre d'esssais " + nombreEssais;
    }

    public void SoumettreNombre()
    {
        if (saisieEssai.text !="")
        {
            // Il faut vérifier si le nombre est trouvé
            nombreEssais = nombreEssais + 1;
            textNbEssais.text = "Nombre d'esssais " + nombreEssais;
        }
        else
        {
            textReponse.text = "Entrez une valeur avant de cliquer";

        }
        if (saisieEssai.text = nombreMystere)
        {
            nombreEssais = nombreEssais +1;
            textReponse.text = "Vous avez gagné !";
        }

    }

}

Sorry it's in french, but if any can just help with the problem it would be amaizing.

All replies (8)

Friday, September 25, 2020 10:11 PM

Yeah I understand all of that.
Let me just tell you what I have to do, it's on Unity. So there's a random number generated randomly, that we don't know, only the program knows, and you have to guess the number.

Now all of that works correctly, it's generate a number, I have the " textNbEssais.text" which basically counts my number of attempts, and I have a button to redeem my number I put in the text box. It all works. Now im stuck at adding a " Congratulations ! " when you guess the number correctly, but I can't do it.

    public InputField saisieEssai > Where you type your answer
    public int nombreMystere;   > The random number generated by the core
    public int nombreEssais;       > The amount of tries 
    public Text textNbEssais;       > The amount of tries written
    public Text textReponse;       > The answer of either if you guessed right of not.


Saturday, September 26, 2020 5:51 PM

There are two errors in this line:

if (saisieEssai.text = nombreMystere)

The first one is that you should use == (comparison) instead of = (assignment).

It currently complains about the assignment because you are assigning the nombreMystère (which is an int) to a text property (which presumably is of type string).

You can fix it in this way:

if (saisieEssai.text == nombreMystere.ToString())

Note that the previous answer where they told you that the error was in the string plus int concatenation is wrong. You CAN concatenate a string with an int in C#; the concatenation operator does perform the implicit conversion of the int into string. So the mistake is not there.

EDITED: Since apparently some readers have objected to the preceding, I am quoting from this page of the C# specification:

"These overloads of the binary + operator perform string concatenation. [...], any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object."

Note that this is the language specification according to ECMA-334 and ISO/IEC 23270 standards. It is not allowable to have this behavior modified in a specific dialect or implementation and still call it C#.


Saturday, September 26, 2020 6:02 PM | 1 vote

The error regarding implicit conversion is probably caused by specific functioning of string concatenation in the dialect of C#, implemented in Unity or Mono. Maybe you confirm this is some dedicated forums.


Saturday, September 26, 2020 8:15 PM

Andrew, please don't call people stupid, or any other names for that matter. Everyone should try to be civil on these forums.

Sometimes it seems that "common" courtesy is not all that common.  =0(

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Saturday, September 26, 2020 11:31 PM

Your post is being reported. Your language in this thread is unacceptable. Additionally, you're providing false information.


Sunday, September 27, 2020 1:35 AM | 1 vote

The error regarding implicit conversion is probably caused by specific functioning of string concatenation in the dialect of C#, implemented in Unity or Mono. Maybe you confirm this is some dedicated forums.

It's actually confirmable in Visual Studio if you turn on Option Strict.  Alberto Pablacion has no business being a Moderator on a C# Forum when he's stupid enough to make totally ignorant statements as if he had authority to be believed.

Before you can learn anything new you have to learn that there's stuff you don't know.

@Andrew, can you please refrain from saying things like Alberto has no business being a moderator and also saying he is stupid which is so far from the truth.

In regards to "If you turn On Option Strict", there is no Option Strict other than with VB.NET.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow


Sunday, September 27, 2020 11:11 AM

The error regarding implicit conversion is probably caused by specific functioning of string concatenation in the dialect of C#, implemented in Unity or Mono. Maybe you confirm this is some dedicated forums.

It's actually confirmable in Visual Studio if you turn on Option Strict.  Alberto Pablacion has no business being a Moderator on a C# Forum when he's stupid enough to make totally ignorant statements as if he had authority to be believed.

Before you can learn anything new you have to learn that there's stuff you don't know.

@Andrew, can you please refrain from saying things like Alberto has no business being a moderator and also saying he is stupid which is so far from the truth.

In regards to "If you turn On Option Strict", there is no Option Strict other than with VB.NET.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

There's a similar option to turn off use of 'var' and such in C#.  I have brain damage, PTSD, and a seizure disorder.  What's Pablo or Randolph West's excuse?

Before you can learn anything new you have to learn that there's stuff you don't know.


Monday, September 28, 2020 3:35 PM | 1 vote

`var` in C# is more like declaring a local variable with `Dim` in Visual Basic without specifiying the type.

The compiler will infer the type of the variable, but will not have any difference in conversions for declaring the type.

Paulo Morgado