Re-declare??

B

Bob Hollness

Hi all.

I have a Sub that calls another sub. Both subs use a common object, so I
used Public to declare it at the top of the module, as below.

Public Writer As StreamWriter = File.CreateText(Application.StartupPath &
"\Update.txt")

The first time my Sub runs it works fine. But the second time it fails
telling me "Cannot write to a closed TextWriter. I am guessing that this is
because the Public section is only read once instance of the application. I
have to close the TextWriter so that I can use the file in another process.
Any idea how I can either ensure that the Public section is re-read, or,
provide the TextWriter to the 2nd Sub?
 
C

Chris, Master of All Things Insignificant

I think I'm a little confused at what you need to do. But your problem lies
in

If you declare like this:

Public Writer As StreamWriter

Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub

Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub

Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub

Now this way of doing it opens the file if it was closed. And uses the same
file if it was already opened. I think this is what you are talking about
doing. If not, can you post some sample code that shows the problem more?

Chris
 
B

Bob Hollness

Thanks for the reply. This is my code. As you can see, it recurses by
calling itself repeatedly. And then the sub ReadAllFiles tidies up by
closing the stream. My problem is that the 2nd time it runs it errors on
line ".WriteLine(sFile) in the ReadDirs Sub.


Sub ReadAllFiles

Call ReadDirs(WHATEVER_PATH, True)

WriteTxtUpdate.Flush
WriteTxtUpdate.Close

End Sub


Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)

Sourcedir &= Path.DirectorySeparatorChar

If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive)
sDirInfo = Nothing
Next
End If

With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With

End Sub

--
Bob Hollness

-------------------------------------
I'll have a B please Bob

Chris said:
I think I'm a little confused at what you need to do. But your problem
lies in

If you declare like this:

Public Writer As StreamWriter

Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub

Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub

Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub

Now this way of doing it opens the file if it was closed. And uses the
same file if it was already opened. I think this is what you are talking
about doing. If not, can you post some sample code that shows the problem
more?

Chris
 
C

Chris, Master of All Things Insignificant

You are talking about "2nd time" meaning the second time you call the
function ReadAllFiles right. If that is the case then my solution in my
previous post solves the problem. After you close WriteTextUpdate you have
to open it again before you use it. Two ways to solve it.

1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs
2. (Probably a better way for this case, as I stated before) Define then
object WriteTextUpdate as public but open it in ReadAllFiles

Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles

WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)

WriteTxtUpdate.Flush
WriteTxtUpdate.Close

End Sub

You won't get an error this way, but the old file is destoyed w/ every call.
Chris


Bob Hollness said:
Thanks for the reply. This is my code. As you can see, it recurses by
calling itself repeatedly. And then the sub ReadAllFiles tidies up by
closing the stream. My problem is that the 2nd time it runs it errors on
line ".WriteLine(sFile) in the ReadDirs Sub.


Sub ReadAllFiles

Call ReadDirs(WHATEVER_PATH, True)

WriteTxtUpdate.Flush
WriteTxtUpdate.Close

End Sub


Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)

Sourcedir &= Path.DirectorySeparatorChar

If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive)
sDirInfo = Nothing
Next
End If

With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With

End Sub
 
B

Bob Hollness

How can I declare it and then pass it to the Sub? This is new to me...
sorry..... :-(

--
Bob Hollness

-------------------------------------
I'll have a B please Bob

Chris said:
You are talking about "2nd time" meaning the second time you call the
function ReadAllFiles right. If that is the case then my solution in my
previous post solves the problem. After you close WriteTextUpdate you
have to open it again before you use it. Two ways to solve it.

1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs
2. (Probably a better way for this case, as I stated before) Define then
object WriteTextUpdate as public but open it in ReadAllFiles

Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles

WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)

WriteTxtUpdate.Flush
WriteTxtUpdate.Close

End Sub

You won't get an error this way, but the old file is destoyed w/ every
call.
Chris
 
C

Chris, Master of All Things Insignificant

Like I said, I'd probably declare it like the code I showed since you are
doing it recursive and all.


But...

Sub ReadAllFiles


Dim WriteTxtUpdateForThisSub as StreamWriter = File.Create(....)

Call ReadDirs(WHATEVER_PATH, True, WriteTxtUpdateForThisSub)

WriteTxtUpdateForThisSub.Flush
WriteTxtUpdateForThisSub.Close

End Sub


Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean, ByRef
WriteTxtUpdate as StreamWriter)

Sourcedir &= Path.DirectorySeparatorChar

If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive, WriteTxtUpdate)
sDirInfo = Nothing
Next
End If

With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With

End Sub



Bob Hollness said:
How can I declare it and then pass it to the Sub? This is new to me...
sorry..... :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top