从字符串“"转换输入'Integer'无效。 [英] Conversion from string "" to type 'Integer' is not valid.

查看:52
本文介绍了从字符串“"转换输入'Integer'无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

在Vista上使用带有挪威语区域设置的VB2005。


测试程序有3个文本框,总计在txt3中保存。


使用下面的代码,当txt2转换为空时,会导致错误

为空。 txt1中的代码工作正常,但我要问的是有更好的方法

编码这个


公共类主要

Dim y作为整数

Dim x作为整数

Private Sub txt1_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt1。 LostFocus


如果txt1.Text =""然后

y = 0

否则

y = CInt(txt1.Text)

结束如果

End Sub


Private Sub txt2_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt2.LostFocus


''如果txt2为空则会产生错误

x = CInt(txt2.Text)

txt3.Text = x + y

结束次级

结束课程


提前致谢。

-

Dave Griffiths

Hi all
Using VB2005 on Vista with a Norwegian locale setup.

The test program has 3 textboxes the sum held in txt3.

Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this

Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus

If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub

Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus

'' This produces an error if txt2 is empty
x = CInt(txt2.Text)

txt3.Text = x + y
End Sub
End Class

Thanks in advance.
--
Dave Griffiths

推荐答案

7月2日上午10:30,Dave griffiths < davegino ... @ nospam.hotmail.com>

写道:
On Jul 2, 10:30 am, "Dave griffiths" <davegino...@nospam.hotmail.com>
wrote:

大家好

使用带有挪威语区域设置的Vista上的VB2005。


测试程序有3个文本框,总和在txt3中保存。


使用下面的代码,当txt2转换为空时,会导致错误

为空。 txt1中的代码工作正常,但我要问的是有更好的方法

编码这个


公共类主要

Dim y作为整数

Dim x作为整数

Private Sub txt1_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt1。 LostFocus


如果txt1.Text =""然后

y = 0

否则

y = CInt(txt1.Text)

结束如果

End Sub


Private Sub txt2_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt2.LostFocus


''如果txt2为空则会产生错误

x = CInt(txt2.Text)

txt3.Text = x + y $ / $
结束次级

结束班级


提前致谢。


- -

Dave Griffiths
Hi all
Using VB2005 on Vista with a Norwegian locale setup.

The test program has 3 textboxes the sum held in txt3.

Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this

Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus

If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub

Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus

'' This produces an error if txt2 is empty
x = CInt(txt2.Text)

txt3.Text = x + y
End Sub
End Class

Thanks in advance.

--
Dave Griffiths



我会这样做:


If IsNumeric(txt2.Text )然后

x = Integer.Parse(txt2.Text)

否则

x = 0

结束如果


或者即使这样:


Dim x as Integer = 0

Integer.TryParse(txt2.Text,x)


谢谢,


Seth Rowe

I would do this:

If IsNumeric(txt2.Text) Then
x = Integer.Parse(txt2.Text)
Else
x = 0
End If

Or Even this:

Dim x as Integer = 0
Integer.TryParse(txt2.Text, x)

Thanks,

Seth Rowe


7月2日,上午10:42,rowe_newsgroups< r owe_em ... @ yahoo.comwrote:
On Jul 2, 10:42 am, rowe_newsgroups <rowe_em...@yahoo.comwrote:

7月2日上午10:30,Dave griffiths < davegino ... @nospam.hotmail.com>

写道:
On Jul 2, 10:30 am, "Dave griffiths" <davegino...@nospam.hotmail.com>
wrote:

大家好

使用Vista上的VB2005,带有挪威语区域设置。
Hi all
Using VB2005 on Vista with a Norwegian locale setup.


测试程序有3个文本框,即txt3中保存的总和。
The test program has 3 textboxes the sum held in txt3.


使用下面的代码,当txt2转换为空时,会导致错误

为空。 txt1中的代码工作正常,但我要问的是有更好的方法

编码这个
Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this


Public Class Main

Dim y As Integer

Dim x As Integer

Private Sub txt1_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt1.LostFocus
Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus


如果txt1.Text =""然后

y = 0

否则

y = CInt(txt1.Text)

结束如果

End Sub
If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub


Private Sub txt2_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)Handles txt2.LostFocus
Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus


''如果txt2为空,则会产生错误

x = CInt(txt2.Text)
'' This produces an error if txt2 is empty
x = CInt(txt2.Text)


txt3.Text = x + y

End Sub

End Class
txt3.Text = x + y
End Sub
End Class


提前致谢。
Thanks in advance.


-

Dave Griffiths
--
Dave Griffiths



我会这样做:


如果是IsNumeric(txt2.Text)那么

x = Integer.Parse(txt2.Text)

Else

x = 0

结束如果


或者即使这样:


Dim x as整数= 0

Integer.TryParse(txt2.Text,x)


谢谢,


Seth Rowe


I would do this:

If IsNumeric(txt2.Text) Then
x = Integer.Parse(txt2.Text)
Else
x = 0
End If

Or Even this:

Dim x as Integer = 0
Integer.TryParse(txt2.Text, x)

Thanks,

Seth Rowe



我忘了说为什么:-)


使用IsNumeric或TryCast不仅会阻止来自

弹出一个异常,但也会阻止

的非整数值导致问题。例如,如果用户在框中输入文本

(您可能应该禁用此选项),或者如果他们输入的值超出整数变量范围(
)虽然我不认为IsNumeric

会抓住这个,所以你可能会采用tryparse方式)


谢谢,


Seth Rowe

I forgot to say why :-)

Using IsNumeric or TryCast will not only prevent a null string from
popping an exception, but will also stop non-integer values from
causing problems. For example, if the user enters text into the box
(you should probably disable this) or if they enter a value that is
out of range of an integer variable (though I don''t think IsNumeric
will catch this, so you might go the tryparse way)

Thanks,

Seth Rowe


在txt2_LostFocus事件处理程序中,您可以更改x的分配。 to:


x = CInt(Val(txt2.Text))


虽然有些程序员认为Val是实际的不可靠替代品/>
正式检查数据。此外,还有一些小的情况,

Val会产生错误而不是为未记录的输入返回零。


-----

Tim Patrick - www.timaki.com

从头到尾完成Visual Basic 2005
In the txt2_LostFocus event handler, you can change the assignment of "x" to:

x = CInt(Val(txt2.Text))

although some programmers consider Val to be an unsafe alternative to actual
formal checking of the data. Also, there are some minor situations where
Val will generate an error instead of returning zero for unrecogized input.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005

大家好用Vista上的VB2005和挪威语区域设置。


测试程序有3个文本框,总和在txt3中保存。


使用下面的代码,txt2转换导致错误,当它离开

为空时。 txt1中的代码工作正常,但我要问的是有更好的方法

编码这个


公共类主要

Dim y作为整数

Dim x作为整数

Private Sub txt1_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt1。 LostFocus

如果txt1.Text =""然后

y = 0

否则

y = CInt(txt1.Text)

结束如果

End Sub

Private Sub txt2_LostFocus(ByVal sender As Object,ByVal e As

System.EventArgs)处理txt2.LostFocus


''如果txt2为空则会产生错误

x = CInt(txt2.Text)

txt3.Text = x + y

结束分

结束课

提前致谢。
Hi all Using VB2005 on Vista with a Norwegian locale setup.

The test program has 3 textboxes the sum held in txt3.

Using the code below, txt2 conversion causes an error when it is left
empty. The code in txt1 works OK but I am asking is there a better way
of coding this

Public Class Main
Dim y As Integer
Dim x As Integer
Private Sub txt1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt1.LostFocus
If txt1.Text = "" Then
y = 0
Else
y = CInt(txt1.Text)
End If
End Sub
Private Sub txt2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txt2.LostFocus

'' This produces an error if txt2 is empty
x = CInt(txt2.Text)
txt3.Text = x + y
End Sub
End Class
Thanks in advance.



这篇关于从字符串“&quot;转换输入'Integer'无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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