我使用以下代碼解析出測試文件。當我分配file = Read()時,我在Sub Main()中得到了變量轉換錯誤。 Read()的返回值是一個TextFieldParser類型。如何將適當的變量類型分配給「文件」,以便我可以將輸出寫入文本文件?將變量寫入與分配給該變量的函數類型不同的文件
謝謝!
模塊模塊1
Function Read()
Using MyReader As New FileIO.TextFieldParser("C:\Users\Colin\Desktop\Parse_Me.txt")
Dim currentRow As String
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadLine()
Console.WriteLine(Parse_me(currentRow))
Catch ex As FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
" is invalid. Skipping")
End Try
End While
Return MyReader
MyReader.Close()
End Using
End Function
Function Parse_me(ByVal test As String)
Dim Set_1, Set_2, Set_3, Set_4, Set_5 As String
Dim new_string As String
Set_1 = test.Substring(0, 4)
Set_2 = test.Substring(7, 2)
Set_3 = test.Substring(11, 1)
Set_4 = test.Substring(14, 4)
Set_5 = test.Substring(20, 4)
new_string = Set_1 & " " & Set_2 & " " & Set_3 & " " & Set_4 & " " & Set_5
Return new_string
End Function
Sub Main()
Dim file As Object
file = Read()
FilePutObject("C:\Users\Colin\Desktop\Parse_Meoutput.txt", file)
End Sub
前端模塊
'Dim file As FileIO.TextFieldParser'?除非有一些瘋狂的VB規則我不知道? –
我試過了,仍然收到錯誤。從字符串「C:\ Users \ Colin \ Desktop \ Parse_Meo」轉換爲鍵入「整數」無效。 –
問題是'FilePutObject'需要一個整數作爲第一個參數,而你正在傳遞一個字符串。 –