我一直得到输入字符串格式错误 [英] I keep getting input string is in wrong format

查看:73
本文介绍了我一直得到输入字符串格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断得到这个标签只有0写在上面。



我尝试过:



 int HighScore = int.Parse(label3.Text); 

解决方案

< blockquote>结果并非如此:我试过了,你也可以:

 int HighScore = int.Parse(0); 

用一个替换固定的字符串TextBox,它仍然适用于我:

 int HighScore = int.Parse(tbUserInput.Text); 

或标签:

 int HighScore = int.Parse( labHighScore.Text); 

这些都没有给出问题。



所以问题是 label3 不包含您​​的想法。为什么不?它包含什么?有些问题只有你能回答 - 这意味着它取决于你!



幸运的是,你有一个工具可以帮助你找到答案发生了什么:调试器。通过单击该函数在该行中设置断点,然后使用Debug菜单选择Toggle Breakpoint,然后通过调试器运行代码。当它到达断点时,它将停止,并给你控制,这样你就可以通过将鼠标悬停在它们上面来查看包含的东西。

然后查看你的代码,并根据你的数据和工作什么应该手动发生。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一种新的(非常非常有用的)技能:调试!



但我会怀疑你的标签错了......这导致了两个严肃的建议:

1)停止使用Visual Studio默认名称 - 你可能还记得TextBox8是今天的手机号码,但是当你必须在三周内修改它时,你会吗?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键......

2)停止使用int.Parse,然后使用int.TryParse - 它返回一个true / false值来说it working/它不起作用你可以用它来告诉你的用户知道有问题,而不仅仅是崩溃。


I keep getting this the label only has 0 written on it.

What I have tried:

int HighScore = int.Parse(label3.Text);

解决方案

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.


这篇关于我一直得到输入字符串格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆