我们有一个用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)
答案 0 :(得分:0)
错误消息中有15个空格。因此问题可能出在其他地方。 我想可能在这里。
CustomerNumber = InputLine.Substring(57, 15)
也许CustomerNumber应该是一个字符串。如果没有,那么我建议您也对此进行一次TryParse。