I am trying to recreate a flappy bird game from an online tutorial, but I have encountered an issue with my code.
I am trying to set up the program to keep track of my score, but it is not allowing me to convert my int Score variable into a new string(.Text) variable and I have not had luck finding any solutions to this problem.
I also noticed that my using System.Text; is not available as it's grayed out. Could that be the reason and if it is how can I reactivate it or work around it?
Any suggestions on how to overcome this issue would be appreciated.

CodePudding user response:
If you're keeping the score numerically (good idea, easy to increment etc)
int _score = 37;
And you want to show it in a label, you need to convert the int to a string:
scoreLabel.Text = _score.ToString();
Resist the temptation to keep the score as a string and convert it back and forth whenever you need to do math on it:
//no
scoreLabel.Text = (int.Parse(scoreLabel.Text) 1).ToString();
While that might helpfully give you some idea of how you can call methods on data and immediately use it to feed into other things (more methods), roundtripping things back and forth from string "just because a string can usefully represent most things" isn't something we strive for in well engineered programs.. Imagine your boss then wants the word "points" in the score string.. that hack becomes:
scoreLabel.Text = (int.Parse(scoreLabel.Text.Replace(" points", "")) 1).ToString() " points";
Now imagine it has to be localized so "points" is in German or English.. It's just going to get worse and worse ;)
In your screenshot, scoreText is being underlined for a different reason, probably "scoreText does not exist in the current context" - to help with that we'd ideally need to know a bit more about where scoreText is declared (is it even declared?) and as what..
using System.Text; is not available as it's grayed out.
usings gray out for a few reasons, but ultimately it's because they aren't being used. That might be because some necessary reference isn't imported so the code that would use the xxx in using xxx is in error and not being considered "a use", but all in this might well be tangential to the actual problem you face. System.Text contains some useful things like regular expressions and stringbuilder but assigning a value to the Text property of some class (like a Label) isn't necessarily at all related to anything in the System.Text namespace, so it might not have any bearing on your overall problem. For example, if you put a line var sb = new StringBuilder(); that using System.Text; should just light up, because you're now making use of something in the System.Text namespace, but it doesn't help you with the mission of converting an int to a string or even sounding out why you're trying to use a variable that isn't available to the code, at the point of use
