StreamWriter Opening and Closing

I know how to create a StreamWriter like so:

Dim srFileWriter As New System.IO.StreamWriter("RecResults.txt", True)

close it like this


but say I want to reopen that file for more writing after I close it. How do I do that with the same object? You see two functions trade off writing to this I have to close one stream to allow the other to do some writing..then reopen it when the function returns.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Don't close the StreamWriter.  Instead either pass the StreamWriter instance to your functions so you can use the same instance or declare it at a higher level (Form or Global) so it can be accessed from both places.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You might consider writing a disposable class for it.

Imports System.IO

Public Class CFileWriter
    Implements IDisposable

    Private disposedValue As Boolean = False        ' To detect redundant calls
    Private _srFile As StreamWriter

    Public Sub New(ByVal sFileName As String, ByVal append As Boolean)
        _srFile = New StreamWriter(sFileName, append)
    End Sub

    Public Sub Write(ByVal sText As String)
    End Sub

    Public Sub WriteLine(ByVal sText As String)
    End Sub

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If Not _srFile Is Nothing Then _srFile.Dispose()
            End If
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    Public Sub Dispose() Implements IDisposable.Dispose
    End Sub
#End Region

End Class
SGyvesAuthor Commented:
I will probably just go for simplicity for now and learn about the IDispose black magic later.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.