开发者

Write a variable to a file that has a different type than the function assigned to the variable

开发者 https://www.devze.com 2023-04-05 09:29 出处:网络
I have the following code that I am using to parse out a test file.I am getting variable conversion error in Sub Main() when I assign file = Read(). The return value of Read() is a TextFieldParser typ

I have the following code that I am using to parse out a test file. I am getting variable conversion error in Sub Main() when I assign file = Read(). The return value of Read() is a TextFieldParser type. How do I assign the proper variable type to "file" so I can write the output to a text file?

Thanks!

Module Module1

Function Read()
    Using MyReader As 开发者_如何学PythonNew 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

End Module


Here's how FilePutObject is supposed to work (example taken from MSDN documentation for FilePutObject):

Sub WriteData()
    Dim text As String = "test"
    FileOpen(1, "test.bin", OpenMode.Binary)
    FilePutObject(1, text)
    FileClose(1)
End Sub

The 1 act as an identifier for the file. Note also that the file name is passed to FileOpen before calling FilePutObject, and that FileClose is called afterwards. Also note that a string is being written to the file. I don't know which types of data are valid for being passed to FilePutObject, but FileIO.TextFieldParser is definitely not one of them (I just tried it).

Correct me if I'm wrong, but I'm pretty sure that FilePutObject is one of those carry-overs from VB6. If you're writing new code, I would rather use a Stream object for my I/O. For one, it's a lot more .Net-ish (i.e., type-safe, object-oriented, etc). And as far as usability goes, it's a lot clearer how a Stream works, not to mention it doesn't involve passing arbitrary integers as handles to functions in order to identify which file you'd like to work with. And to top it all off, a Stream works whether you want to write to a file, to the console, or send the data to another machine. To sum up, I would definitely look up the Stream class, some of its child classes (like FileStream, and whatever else appeals to you), and some associated types (such as the TextWriter class for conveniently writing text).


Change the definition of the function "read" to:

Function Read() as FileIO.TextFieldParser

and change the declaration of "file" in sub main to:

Dim file as FileIO.TextFieldParser

That way the data type of the function and assignment match.

0

精彩评论

暂无评论...
验证码 换一张
取 消