IsNumeric() 在数字上失败的任何原因?

发布于 2024-07-29 07:30:21 字数 221 浏览 6 评论 0 原文

我目前有这行代码,在过去的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法唧唧 2024-08-05 07:30:22

在将字符串传递给函数之前尝试对字符串进行 Trim() 处理。 此外,您可以尝试以下方法,而不是使用像 IsNumeric 这样的 VB 特定函数:

Dim input as Integer

If Integer.TryParse(txtProductID.Text, input) Then
    ....do stuff with input
Else
    Dim msg as String = "Error!"
End if

如果您的数字是十进制数,则 Double 上有相应的函数以及Single

至于 IsNumeric 失败的具体原因,我无法告诉你。 不过,我可以告诉您,我一直发现坚持使用与语言无关而不是特定于语言的 BCL 兼容函数很有帮助,例如 IsNumericStr

Try Trim()ing the string before passing it into the function. In addition, rather than using a VB-specific function like IsNumeric, you might try an approach like this:

Dim input as Integer

If Integer.TryParse(txtProductID.Text, input) Then
    ....do stuff with input
Else
    Dim msg as String = "Error!"
End if

If your number is a decimal number, there are corresponding functions on Double and Single 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, like IsNumeric, Str, etc.

↘紸啶 2024-08-05 07:30:22

呃...我是个白痴...感谢你们的帮助,但显然我在接受输入之前清除了整个表单,所以“”永远不会作为“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.

梦途 2024-08-05 07:30:21

有点盲目,但需要注意的一件事是,也许有人在同一个类中编写了一个名为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文