IsNumeric() 在数字上失败的任何原因?
我目前有这行代码,在过去的 6 个月里一直在工作:
If IsNumeric(txtProductID.Text) Then
...do stuff
Else
Dim msg As String = "Error!"
End If
突然之间,无论在 txtProductID 中放入什么样的条目(包括纯数字),它都会失败! 我有理由为此疯狂吗?
I currently have this line of code which has been working for the past 6 months:
If IsNumeric(txtProductID.Text) Then
...do stuff
Else
Dim msg As String = "Error!"
End If
All of the sudden, no matter what kind of entry is put in txtProductID
(including plain numbers), it fails! Is there reason for me to be going crazy over this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在将字符串传递给函数之前尝试对字符串进行 Trim() 处理。 此外,您可以尝试以下方法,而不是使用像
IsNumeric
这样的 VB 特定函数:如果您的数字是十进制数,则
Double
上有相应的函数以及Single
。至于
IsNumeric
失败的具体原因,我无法告诉你。 不过,我可以告诉您,我一直发现坚持使用与语言无关而不是特定于语言的 BCL 兼容函数很有帮助,例如IsNumeric
、Str
等Try
Trim()
ing the string before passing it into the function. In addition, rather than using a VB-specific function likeIsNumeric
, you might try an approach like this:If your number is a decimal number, there are corresponding functions on
Double
andSingle
as well.As to the particular reason that
IsNumeric
is failing, I couldn't tell you. I can tell you, though, that I've always found it helpful to stick to BCL-compliant functions that are language-agnostic rather than language-specific, likeIsNumeric
,Str
, etc.呃...我是个白痴...感谢你们的帮助,但显然我在接受输入之前清除了整个表单,所以“”永远不会作为“IsNumeric”传递。 请不要再看这个问题了。 我感觉病了。
再次感谢你的帮助。
ugh... i'm an idiot... thanks for your help guys, but apparently i was clearing my whole form before accepting input, so "" will never pass as "IsNumeric". Please don't look at this question again. I feel ill.
Thanks again for your help.
有点盲目,但需要注意的一件事是,也许有人在同一个类中编写了一个名为 IsNumeric 的私有方法。 您确定上面的代码正在执行 Microsoft.VisualBasic.IsNumeric() 吗? 如果将光标放在 IsNumeric 上并按 F12,定义指向哪里?
Kind of a shot in the dark, but one thing to watch for is that maybe someone wrote a private method called IsNumeric within the same class. Are you sure that the code above is executing Microsoft.VisualBasic.IsNumeric()? If you put your cursor on IsNumeric and hit F12 where does the definition point to?