Click here to Skip to main content
15,845,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I keep getting this the label only has 0 written on it.

What I have tried:

int HighScore = int.Parse(label3.Text);
Posted
Updated 1-Sep-18 20:43pm

1 solution

That turns out not to be the case: I tried, and so can you:
int HighScore = int.Parse("0");
Replace the fixed string with a TextBox and it still works for me:
int HighScore = int.Parse(tbUserInput.Text);
Or a Label:
int HighScore = int.Parse(labHighScore.Text);
None of these give problems.

So, the problem is that label3 doesn't contain what you think it does. Why not? What does it contain? There are questions that only you can answer - which means it's going to be up to you!

Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. Put a breakpoint on the that line in the function by clicking on it, and using the Debug menu to select "Toggle Breakpoint", then run your code through the debugger. When it reaches the breakpoint, it will stop, and give you control so you can look at what things contain by hovering the mouse over them.
Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But I'd suspect you have the wrong label ... and that leads to two serious suggestions:
1) Stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
2) Stop using int.Parse, and use int.TryParse instead - it returns a true / false value to say "it worked" / "it didn't work" and you can use that to klet your user know there is a problem instead of just crashing.
 
Share this answer
 

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