在VB中将空格转换为int会导致错误

时间:2018-10-30 17:13:32

标签: vb.net

我们有一个用VB编写的旧应用程序,用于解析文件。尝试将空格转换为整数时出现错误。我认为程序应该继续并为 LoadNumber 分配0。可能与旧的VB库文件有关吗?因为我在Visual Studio的VB控制台应用程序中运行了代码,所以它似乎运行良好。我如何找出导致问题的原因?

Overridable Sub Parse01()
BOLNumber = InputLine.Substring(12, 20)
SourceNumber = InputLine.Substring(2, 5)

Carrier = InputLine.Substring(116, 6)
Driver = InputLine.Substring(37, 15)
DateFinished = InputLine.Substring(77, 8)
TimeFinished = InputLine.Substring(87, 2) & ":" & InputLine.Substring(89, 2)
Finish_DateTime = DateFinished.ToString & " " & TimeFinished
TruckNumber = InputLine.Substring(124, 5)

'LoadNumber = InputLine.Substring(107, 7)

Dim loadNumberAsInt As Integer
If Integer.TryParse(InputLine.Substring(107, 7), loadNumberAsInt) Then
    LoadNumber = InputLine.Substring(107, 7)
Else
    LoadNumber = "0"
End If

CustomerNumber = InputLine.Substring(57, 15)
TimeIn = InputLine.Substring(91, 4)

End Sub

错误

Could not process the file for shipper:0001 - Conversion from string "               " to type 'Integer' is not valid. -    at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
       at Program.Reader.Parse01()
       at Program.Reader.ProcessRecords()
       at Program.Reader.ParseInputLine()
       at Program.Reader.Parse(String Path)
       at Program.Reader.ProcessFile(String Source, String Number, EDI_File_Reader EDI_R)

1 个答案:

答案 0 :(得分:0)

错误消息中有15个空格。因此问题可能出在其他地方。  我想可能在这里。

CustomerNumber = InputLine.Substring(57, 15)

也许CustomerNumber应该是一个字符串。如果没有,那么我建议您也对此进行一次TryParse。