G
Guru
Michael D. Ober said:Guru said:Michael D. Ober said:OK maybe I'm missing something obvious.
Look at the following code:
Using fs As New
FileStream(My.Computer.FileSystem.SpecialDirectories.Desktop &
"\junk.txt",
FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Using sw As New StreamWriter(fs,
System.Text.Encoding.ASCII)
sw.WriteLine("abc")
sw.WriteLine("def")
sw.WriteLine("ghi")
End Using
Using bw As New BinaryWriter(fs) 'throws an error right
here
bw.Write(CByte(&H4A)) 'J
bw.Write(CByte(&H4B)) 'K
bw.Write(CByte(&H4C)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
bw.Write(CByte(&H4D)) 'J
bw.Write(CByte(&H4E)) 'K
bw.Write(CByte(&H4F)) 'L
bw.Write(CByte(&HA)) '[CR]
bw.Write(CByte(&HD)) '[LF]
End Using
End Using
It crashes on the line indicated with a "Stream not writeable"
exception.
I'm assuming the StreamWriter is closing the underlying FileStream
when it
is disposed. Where's the Filestream.Open command??
--
David Streeter
Synchrotech Software
Sydney Australia
You whacking great ignoramus. Do you get paid for writing code? Learn
about scope, you less than useless spaghetti-jockey; the freaking
streamwriter is derefenced as soon as the Using sw block terminates.
For crying out loud. BWAHAHAHAHAH!
Maybe you should look at the code again - the BinaryWriter isn't based
on the StreamWriter<BITCHSLAP>
Nitwit. The problem is, as I already asserted, caused by the Using sw
clause.
When the StreamWriter is disposed by the End Using on its head-long rush
into the total oblivion of Nothingness, it takes the FileStream along for
the ride; even Blind Bart can see that the BinaryWriter is trying to
reference the now obliterated FileStream.
Need proof? Yes? No? Don't know? Don't care? Considering suicide?
fs is a FileStream
{
sw is a StreamWriter based on fs
{
}
bw is a BinaryWriter based on fs
{
}
}
Maybe you should look at the code again - it is written in VB, not C.
That's your free cloo, clooboi. Don't take up coding for a living; you'll
starve.
The fix is simple. Change Using sw... into Dim sw... and remove the block
end. The intelligent reader, that would obviously not include you, will
note that the code now executes correctly; viz. the StreamWriter is not
disposed, therefore the FileStream is not disposed.
HTH, BegginerBoi.
Guru,
You're an ass. My pseudo code was<BITCHSLAP>
Note how you have been forced into a defensive posture.
By the way, since you are unable to post using your real name<BITCHSLAP>
Nutfuch. Guru _is_ my name, as in Guru Sandaramurthy.
you're not only an ass but also a coward.
What law of what land requires anyone to post with their real name, oh
pathologically obsessive one?
BTW, that you now feel the need to defend your pseudo-code, taken together
with your exceedingly limp-wristed attack on such an irrelevant point as
posting with ones 'real name', the fact of your obsequious and acquiescent
personality is pretty much stitched up.