VB.NET:输入字符串的格式不正确

发布于 2024-10-05 13:55:02 字数 230 浏览 1 评论 0原文

使用以下代码片段,

Foo = IIf(String.IsNullOrEmpty(txtFoo.Text), 0, Integer.Parse(txtFoo.Text.Trim))

当我提交没有值的字段时,出现错误:“输入字符串的格式不正确。” 我没有任何空间或其他东西,并且 String.IsNullOrEmpty(txtFoo.Text) 返回 true。怎么了? 谢谢。

With the following snippet

Foo = IIf(String.IsNullOrEmpty(txtFoo.Text), 0, Integer.Parse(txtFoo.Text.Trim))

I get the error when I submit the field without a value: "Input string was not in a correct format."
I don't have any space or something else and String.IsNullOrEmpty(txtFoo.Text) returns true. What is wrong?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

淡看悲欢离合 2024-10-12 13:55:02

IIF 将评估:

Integer.Parse(txtFoo.Text.Trim) 

无论:

String.IsNullOrEmpty(txtFoo.Text) 

是否为真(因为它只是一个传递给它的三个参数的函数,所以所有参数都必须有效)。因此,即使 txtFoo.text 为空,在这种情况下它仍然尝试将其解析为整数。

如果您使用的是 VS2008,则可以使用 IF 运算符,这将像您期望 IIF 那样短路。

IIF will evaluate:

Integer.Parse(txtFoo.Text.Trim) 

irrespective of whether:

String.IsNullOrEmpty(txtFoo.Text) 

is true or not (as it is just a function with three arguments passed to it, so all arguments must be valid). So even if txtFoo.text is empty, it's still trying to Parse it to an Integer in this case.

If you're using VS2008, you can use the IF operator instead which will short-circuit as you're expecting IIF to do.

娇妻 2024-10-12 13:55:02

IIf 是函数调用而不是真正的条件运算符,这意味着必须对两个参数进行求值。因此,如果您的字符串为 Null/Nothing,它只会尝试调用 Integer.Parse()。

如果您使用的是 Visual Studio 2008 或更高版本,只需一个字符的差异即可解决您的问题:

Foo = If(String.IsNullOrEmpty(txtFoo.Text), 0, Integer.Parse(txtFoo.Text.Trim())

此版本的 If 关键字实际上是一个真正的条件运算符,它将执行以下操作的短路计算:参数如预期。

如果您使用的是 Visual Studio 2005 或更早版本,请按如下方式修复:

If String.IsNullOrEmpty(txtFoo.Text) Then Foo = 0 Else Foo = Integer.Parse(txtFoo.Text.Trim())

IIf is a function call rather than a true conditional operator, and that means both arguments have to be evaluated. Thus, it just attempts to call Integer.Parse() if your string is Null/Nothing.

If you're using Visual Studio 2008 or later, it's only a one character difference to fix your problem:

Foo = If(String.IsNullOrEmpty(txtFoo.Text), 0, Integer.Parse(txtFoo.Text.Trim())

This version of the If keyword is actually a true conditional operator that will do the short-circuit evaluation of arguments as expected.

If you're using Visual Studio 2005 or earlier, you fix it like this:

If String.IsNullOrEmpty(txtFoo.Text) Then Foo = 0 Else Foo = Integer.Parse(txtFoo.Text.Trim())
久夏青 2024-10-12 13:55:02

IIf 不是真正的三元运算符,它实际上计算两个参数表达式。您可能想改用 If 运算符(VS 2008+)。

你会简单地说

If(String.IsNullOrEmpty(txtFoo.Text), 0, Integer.Parse(txtFoo.Text.Trim()))

IIf is not a true ternary operator, it actually evaluates both parameter expressions. You probably want to use the If operator instead (VS 2008+).

You'd simply say

If(String.IsNullOrEmpty(txtFoo.Text), 0, Integer.Parse(txtFoo.Text.Trim()))
假情假意假温柔 2024-10-12 13:55:02

条件部分和“else”部分之间的一个区别是字符串的修剪。您可以在调用 IsNullOrEmpty 之前尝试修剪字符串。

Foo = IIf(String.IsNullOrEmpty(txtFoo.Text.Trim), 0, Integer.Parse(txtFoo.Text.Trim))

One difference between the conditional and the "else" portion is the trimming of the string. You could try trimming the string before calling IsNullOrEmpty.

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