473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re-declare??

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?

--
Bob Hollness

-------------------------------------
I'll have a B please Bob
Nov 21 '05 #1
10 2130
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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

Nov 21 '05 #2
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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

Nov 21 '05 #3
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, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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


Nov 21 '05 #4
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, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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


Nov 21 '05 #5
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" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl... 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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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



Nov 21 '05 #6
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" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl... 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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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



Nov 21 '05 #7
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, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:ul**************@TK2MSFTNGP14.phx.gbl...
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" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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



Nov 21 '05 #8
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, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:ul**************@TK2MSFTNGP14.phx.gbl...
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" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
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?

--
Bob Hollness

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



Nov 21 '05 #9
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" <bo*@blockbuster.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl...
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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:ul**************@TK2MSFTNGP14.phx.gbl...
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" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
> 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?
>
> --
> Bob Hollness
>
> -------------------------------------
> I'll have a B please Bob
>



Nov 21 '05 #10
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" <bo*@blockbuster.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl...
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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:ul**************@TK2MSFTNGP14.phx.gbl...
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" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
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, Master of All Things Insignificant" <chris@No_Spam_Please.com>
wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...
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
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl...
> 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?
>
> --
> Bob Hollness
>
> -------------------------------------
> I'll have a B please Bob
>



Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.