2010-07-28 26 views
0

這裏是例外情況:解析字符串VB.NET時出現異常

「輸入字符串格式不正確」。

下面是代碼行:

.Add(Integer.Parse(LoanData.Item("IsApplicantRaceAmericanIndian")).ToString) 
+0

你的問題是什麼? – 2010-07-28 19:23:05

+2

我們需要先看看LoanData.Item()返回什麼,然後才能提供幫助。它顯然返回一個Integer.Parse()無法解析的值。 – 2010-07-28 19:24:25

+0

它爲什麼扔它? – Scott 2010-07-28 19:24:33

回答

4

你試圖解析不能代表有效的整數文本。例如,它可能是「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 
0

有一個小竅門,Integer.Parse不會處理空或空字符串。如果您使用的是.NET 2.0或更新版本,請嘗試使用Integer.TryParse。