Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am developing a game and this game saves scores in variables,the scoreboard is always visible on the form and keeps on updating each time a player wins, now i cant really figure out how i can display these scores. the code i developed gives a conversion error
what am i doing wrong here....
C#
int player = 0; //variable//
            if (cnt == 4)   //if this condition is met//
                player++;   //value of the variable should be incremented//
                textBox1.text = player;  //value of the variable should be displayed on the form//


code samples or alternatives would help
Posted

Another variation is:
C#
textBox1.text = player.ToString();

A slight difference in this version is that you can define the format for the number presentation. For example, you can define the format as:
C#
textBox1.text = player.ToString("#00");

And the result with number 4 would be "04".

More info: Int32.ToString Method [^]
 
Share this answer
 
You are directly assigning an integer value to a string.
This won't work until you type cast it into a string before assignment.

Try textBox1.text = Convert.ToString(player);
 
Share this answer
 
v2
Comments
morojele 7-Sep-11 14:23pm    
THANKS MAN,IT WORKS "Accept Solution"
Yvan Rodrigues 7-Sep-11 15:52pm    
Just to clarify, this is conversion, not casting. A cast would be like textBox1.Text = (string)player, which wouldn't compile, because int can't be cast to string. By using Convert (above) or object.ToString() (below) a conversion method is being invoked to make a string from the value. It is also a good time to point out that it is good practice, whenever creating a new class, to implement (override) the ToString() method if an instance can logically be expressed as a string.
Abhinav S 8-Sep-11 1:28am    
Using toString() directily is never a good idea when compared to Convert.ToString().

If the value was null accidentally, ToString() would throw an error.
Yvan Rodrigues 8-Sep-11 8:48am    
I would disagree. In the case of reference types, like int above, using the structure's ToString() method is more readable, produces less IL code and will never result in a NullReferenceException. For String, you're unlikely to call ToString() so it's a non-issue; and for reference types that don't implement IConvertible you can't use Convert anyways.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900