vb.net 将字符串 "" 转换为类型 'Integer' 无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25057376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:02:35  来源:igfitidea点击:

convert string "" to type 'Integer' is not valid

vb.net

提问by user3458266

Why do I get Conversion from string "" to type 'Integer' is not valid.? Match is a string, and when I compare it to "" it gives me this error. The program works when values are entered etc. but if I want to check for an empty string (the last ElseIf) I get an error.

为什么我得到Conversion from string "" to type 'Integer' is not valid.?Match 是一个字符串,当我将它与 "" 进行比较时,它给了我这个错误。该程序在输入值等时工作,但如果我想检查空字符串(最后一个 ElseIf),我会收到错误消息。

Dim total As Double
Dim Match As String = ""

If Match.EndsWith("HA") OrElse Match.Contains("ha") OrElse Match.Contains("Ha") Then
    Dim nonNumericCharacters As New Regex("\D")
    Dim numericOnlyString As String = nonNumericCharacters.Replace(Match, String.Empty)
    total = numericOnlyString * 10000
ElseIf Match.Contains("Ha m2") Then
    Dim nonNumericCharacters As New Regex("\D")
    Dim numericOnlyString As String = nonNumericCharacters.Replace(Match, String.Empty)
    total = numericOnlyString * 10000
ElseIf Match.Contains("m2") OrElse Match.Contains("sqm") Then
    Dim nonNumericCharacters As New Regex("\D")
    Dim numericOnlyString As String = nonNumericCharacters.Replace(Match, String.Empty)
    total = Val(numericOnlyString)
ElseIf CInt(Match) = 0 OrElse Match = "" Then
    total = Val(String.Empty)
Else
    total = Match
End If
MessageBox.Show(total)

回答by Grim

As commented by Bj?rn-Roger Kringsj?, you need to re-order the expression to check for 'bad' strings first. Also, you can't set the totalto the Valof an empty string. Your code could be adjusted to;

正如 Bj?rn-Roger Kringsj? 所评论的,您需要重新排序表达式以首先检查“坏”字符串。此外,您不能设置totalVal空字符串。您的代码可以调整为;

...
ElseIf Match = "" OrElse CInt(Match) = 0 Then
    total = 0
Else
...

However, in order to let you learn a few things, I would re-write that section of code like this;

但是,为了让你学到一些东西,我会像这样重写那段代码;

Dim ParseResult As Integer    
...
ElseIf String.IsNullOrWhiteSpace(Match) OrElse Not Integer.TryParse(Match, ParseResult) Then
    total = 0
Else
...

This first checks if the Matchstring is Nothing, empty or just spaces/tabs/etc, then if not, tries to parse it to an Integer. If that fails, then the total is set to zero. You should of course do it all with Integer.TryParseand use the result, but I hope that helps.

这首先检查Match字符串是否为空、空或只是空格/制表符/等,然后如果不是,则尝试将其解析为整数。如果失败,则总数设置为零。你当然应该Integer.TryParse用结果来做这一切,但我希望这会有所帮助。

回答by Lasse V. Karlsen

You get "Conversion from string "" to type 'Integer' is not valid." because the conversion from string "" to type 'Integer' is not valid.

您会收到“从字符串“”到类型“整数”的转换无效。” 因为从字符串 "" 到类型 'Integer' 的转换是无效的。

"" is not a valid number.

"" 不是有效数字。

You need to detect this case and pick the right number yourself, like 0, but attempting the conversion will give you that exception.

您需要检测这种情况并自己选择正确的数字,例如 0,但尝试转换会给您带来异常。