我正在开发一个项目,允许孩子们向圣诞老人发送消息。不幸的是,如果他们在 AGE 字段中输入字符串而不是整数,则程序会崩溃并返回 Conversion from string "[exampleString]" to type 'Double' is not valid。
有没有办法检查他们是否输入了整数?这是代码。
If childAge > 0 And childAge < 150 Then
fmSecA2 = "Wow! You are already " & childAge & " years old? You're growing to be a big " & childGender & " now! "
Else
fmSecA2 = "Erm, I couldn't really understand your age. Are you making this up? Ho ho ho!"
End If
谢谢,
凯:)
一个非常简单的技巧是尝试解析 http://msdn.microsoft.com/en-us/library/f02979c7.aspx字符串作为整数。如果成功,它是一个整数(惊喜)。
Dim childAgeAsInt As Integer
If Integer.TryParse(childAge, childAgeAsInt) Then
' childAge successfully parsed as Integer
Else
' childAge is not an Integer
End If
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)