【发布时间】:2011-11-19 11:09:58
【问题描述】:
我有以下用于解析测试文件的代码。当我分配 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”到类型“Integer”的转换无效。
-
问题是
FilePutObject需要一个整数作为第一个参数,而您传递的是一个字符串。
标签: vb.net file variables variable-assignment