将String转换为Integer时出错 [英] Errors in converting String to Integer

查看:584
本文介绍了将String转换为Integer时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串转换为整数。



使用的代码是:



  Dim  PStab  As  整数 
PStab = Convert.ToInt32(P1Stab1.Text)





P1Stab1是一个标签。



当我加载表单时,我收到此错误;



mscorlib.dll中出现System.FormatException类型的异常但未在用户代码中处理

附加信息:输入字符串的格式不正确。




在另一个子例程中,我使用了 Convert.ToInt32()将18个标签的内容转换为整数(包括Convert.ToInt32(P1Stab1.Text))的问题,以便我可以将它们一起添加。使用的代码是:



 公共 PlayersTotal  As   Integer  

PlayersTotal = Convert.ToInt32(P1Stab1.Text)+ Convert.ToInt32(P1Stab2。文本)+ Convert.ToInt32(P1Stab3.Text)+ Convert.ToInt32(P1Stab4.Text)+ Convert.ToInt32(P1Stab5.Text)+ Convert.ToInt32(P1Stab6.Text)+ Convert.ToInt32(P1Stab7.Text)+转换。 ToInt32(P1Stab8.Text)+ Convert.ToInt32(P1Stab9.Text)+ Convert.ToInt32(P1Stab10.Text)+ Convert.ToInt32(P1Stab11.Text)+ Convert.ToInt32(P1Stab12.Text)+ Convert.ToInt32(P1Stab13.Text )+ Convert.ToInt32(P1Stab14.Text)+ Convert.ToInt32(P1Stab15.Text)+ Convert.ToInt32(P1Stab16.Text)+ Convert.ToInt32(P1Stab17.Text)+ Convert.ToInt32(P1Stab18.Text)





所以任何人都可以帮忙解决问题吗?



注意:当时加载表单标签默认为0(零)

解决方法案例

从标签返回的文本不能转换为整数。就这么简单。



您的设计存在一些主要问题。第一个是为什么要将LABEL中的文本(应该是静态内容)转换为值?为什么不使用数据模型来保存此信息?您不应该通过可视控件将数据从一段代码传输到另一段代码!



您需要在代码中拥有一个管理和维护数据的数据结构和它的国家。将UI控件视为向用户显示该状态的单向媒体。它们不能用于传输数据或状态信息。这使得维护数据难以维护和调试。



另一个大问题是Convert对它的内容有一些严格的规则,并且不会转换为整数。如果你在那里有一个逗号或小数点或当前符号,它将失败。请改用Integer.TryParse。


P1Stab1.Text中的任何内容都无法转换为整数。如果您说P1Stab1.Text中的内容,那么有人可能会建议原因,或者如果您使用调试工具,您可能能够找出原因,但我们无法访问您的系统,因此无法查看对于你。


这很简单:你的用户键入错误。

Convert.ToInt32尝试转换eth字符串,但如果找到Hello,123? 或6.7如果无法将其转换为整数值,则抛出异常。

使用Integer.TryParse而不是使用Convert:

< pre lang =VB.NET> Dim PStab As 整数
如果 整数 .TryParse(P1Stab1.Text,PStab)然后
' 向用户报告问题
...
返回
结束 如果


I am trying to convert a string to an integer.

The code used is;

Dim PStab As Integer
        PStab = Convert.ToInt32(P1Stab1.Text)



P1Stab1 is a label.

When I load the form I get this error;

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.


In another subroutine I have used Convert.ToInt32() with no problems to convert the contents of 18 labels to integer (including Convert.ToInt32(P1Stab1.Text)) so I can add them together. The code used is;

Public PlayersTotal As Integer

PlayersTotal = Convert.ToInt32(P1Stab1.Text) + Convert.ToInt32(P1Stab2.Text) + Convert.ToInt32(P1Stab3.Text) + Convert.ToInt32(P1Stab4.Text) + Convert.ToInt32(P1Stab5.Text) + Convert.ToInt32(P1Stab6.Text) + Convert.ToInt32(P1Stab7.Text) + Convert.ToInt32(P1Stab8.Text) + Convert.ToInt32(P1Stab9.Text) + Convert.ToInt32(P1Stab10.Text) + Convert.ToInt32(P1Stab11.Text) + Convert.ToInt32(P1Stab12.Text) + Convert.ToInt32(P1Stab13.Text) + Convert.ToInt32(P1Stab14.Text) + Convert.ToInt32(P1Stab15.Text) + Convert.ToInt32(P1Stab16.Text) + Convert.ToInt32(P1Stab17.Text) + Convert.ToInt32(P1Stab18.Text)



So can anyone help with what is going wrong?

Note: At the time of loading the form the labels are defaulted to 0(zero)

解决方案

The text coming back from the label is not convertible to an integer. It's that simple.

There are some major problems with your design. The first of which is why are you converting the text in a LABEL, which should be static content, to a value? Why are you not using a data model to hold this information instead? You should not be transferring data from one piece of code to another through a visual control!

You need to have a data structure in your code that manages and maintains your data and its state. Think of UI controls as a one-way medium to display that state to the user. They are NOT to be used to transfer data or state information. This makes maintaining the data difficult to maintain and debug.

The other big problem is that Convert has some strict rules on what it will and will not convert to an Integer. If you've got a comma in there or a decimal point or a current symbol, it'll fail. Use Integer.TryParse instead.


Whatever is in "P1Stab1.Text" can't be converted to an integer. If you say what is in P1Stab1.Text then someone might be able to suggest a reason why, or if you use the debugging tools you might be able to work out why yourself, but we can't access your system so can't look for you.


It's simple: your user typed wrong.
Convert.ToInt32 tries to convert eth string, but if it finds "Hello", "123?", or "6.7" if can't convert it to an integer values, so it throws an exception.
Instead of using Convert, use Integer.TryParse:

Dim PStab As Integer
If Not Integer.TryParse(P1Stab1.Text, PStab) Then
	' Report problem to user
        ...
	Return
End If


这篇关于将String转换为Integer时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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