代码之家  ›  专栏  ›  技术社区  ›  Scott

分析字符串VB.NET时出现异常

  •  0
  • Scott  · 技术社区  · 14 年前

    “输入字符串的格式不正确。”

    以下是代码行:

    .Add(Integer.Parse(LoanData.Item("IsApplicantRaceAmericanIndian")).ToString)
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   Dan Tao    14 年前

    您试图分析的文本不能表示有效的整数。例如,它可能是“ABC”,也可能是空白。

    Integer.TryParse 而不是 Integer.Parse

    Dim text As String = LoanData.Item("IsApplicantRaceAmericanIndian")).ToString()
    
    Dim value As Integer
    If Integer.TryParse(text, value)
        .Add(value)
    Else
        ' The text could not be parsed. '
        ' Notify the user, log it, do whatever you like. '
    End If
    
        2
  •  0
  •   Diego Pereyra    14 年前

    作为提示,Integer.Parse不会处理空字符串或空字符串。如果您使用的是.NET 2.0或更新版本,请尝试使用Integer.TryParse。