By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,117 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Catch problem

P: n/a
WROX "Beginning VB.Net" 2nd Edition

Using this code from Chapter 11 Page 472:
Do
Try
' Read a line from the file...
currentLine = myReader.ReadLine
lineCounter = lineCounter + 1
Catch e As EndOfStreamException
' Display the message...
MessageBox.Show(e.Message)
' Exit the Do loop...
Exit Do
Finally
' Concatenate the data to the strData
variable...
currentData = currentData & currentLine & vbCrLf
End Try
Loop While currentLine <> Nothing

The text file I am reading has blank lines in it.
Sample:

Line one <CrLf>
Line two <CrLf>
<CrLf>
Line Four <CrLf>
Line Five <CrLf>
Line Six <CrLf>

When the code reaches Line Four it exit the loop because
currentLine = Nothing.

I want to read all the lines (one at a time) and exit the loop
when I have reached the end of the file. I still want to read past the blank
lines.

If I use a straight loop without the "While currentLine <>
Nothing" the "Catch e As EndOfStreamException" does not catch the end of
stream exception and enters an infinite loop!
How can I read the stream, one line at at time, and process
"ALL" the lines in the file?


Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Doug Gifford" <do*********@cox.net> wrote...
Loop While currentLine <> Nothing


Try:

Loop Until currentLine is Nothing

Tom
Nov 20 '05 #2

P: n/a
Hi Doug,

I'm just wondering whether you could do this and encapsulate the whole thing
in an appropriate try/catch block. EOF is "end of file" -- perhaps this is
what you really want to be testing for...

while not EOF(1)
your code...
loop
I'm a newbie at VB.NET, but it might be worth a try.
Sincerely,
Davis Bennett

P.S. If you try this, let me know how you make out
"Doug Gifford" <do*********@cox.net> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
WROX "Beginning VB.Net" 2nd Edition

Using this code from Chapter 11 Page 472:
Do
Try
' Read a line from the file...
currentLine = myReader.ReadLine
lineCounter = lineCounter + 1
Catch e As EndOfStreamException
' Display the message...
MessageBox.Show(e.Message)
' Exit the Do loop...
Exit Do
Finally
' Concatenate the data to the strData
variable...
currentData = currentData & currentLine & vbCrLf End Try
Loop While currentLine <> Nothing

The text file I am reading has blank lines in it.
Sample:

Line one <CrLf>
Line two <CrLf>
<CrLf>
Line Four <CrLf>
Line Five <CrLf>
Line Six <CrLf>

When the code reaches Line Four it exit the loop because
currentLine = Nothing.

I want to read all the lines (one at a time) and exit the loop
when I have reached the end of the file. I still want to read past the blank lines.

If I use a straight loop without the "While currentLine <>
Nothing" the "Catch e As EndOfStreamException" does not catch the end of
stream exception and enters an infinite loop!
How can I read the stream, one line at at time, and process
"ALL" the lines in the file?


Nov 20 '05 #3

P: n/a
"Doug Gifford" <do*********@cox.net> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
.. . .

OK, I'm new here too (using VB.Net 2003), but here goes
nothing...
Dim MyReader As New System.IO.StreamReader("d1.dat")
Dim sLine as String

sLine = MyReader.ReadLine()
Do While Not ( sLine Is Nothing )
' Do something

' Next Line
sLine = MyReader.ReadLine()
Loop

Seems to handle blank lines.

HTH,
Phill W.
Nov 20 '05 #4

P: n/a
Doug,
I normally follow a more 'positive' version of Phill's example:

Dim MyReader As New System.IO.StreamReader("d1.dat")
Dim sLine as String

Dim sb As New System.Text.StringBuilder

sLine = MyReader.ReadLine()

Do Until sLine Is Nothing
' Do something
lineCounter = lineCounter + 1 sb.Append(currentLine)
sb.Append(vbCrLf)

' Next Line
sLine = MyReader.ReadLine()
Loop
currentData = sb.ToString()

Which as Phill stated, handles blank lines.

StreamReader.ReadLine will return Nothing when the end of stream is found,
there is no "real" need to catch the EndOfStreamException to detect EOF.

Also notice that I am using a System.Text.StringBuilder to create the
currentData string, as concatenating strings in a loop is normally a bad
idea.

Hope this helps
Jay

"Doug Gifford" <do*********@cox.net> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl... WROX "Beginning VB.Net" 2nd Edition

Using this code from Chapter 11 Page 472:
Do
Try
' Read a line from the file...
currentLine = myReader.ReadLine
lineCounter = lineCounter + 1
Catch e As EndOfStreamException
' Display the message...
MessageBox.Show(e.Message)
' Exit the Do loop...
Exit Do
Finally
' Concatenate the data to the strData
variable...
currentData = currentData & currentLine & vbCrLf End Try
Loop While currentLine <> Nothing

The text file I am reading has blank lines in it.
Sample:

Line one <CrLf>
Line two <CrLf>
<CrLf>
Line Four <CrLf>
Line Five <CrLf>
Line Six <CrLf>

When the code reaches Line Four it exit the loop because
currentLine = Nothing.

I want to read all the lines (one at a time) and exit the loop
when I have reached the end of the file. I still want to read past the blank lines.

If I use a straight loop without the "While currentLine <>
Nothing" the "Catch e As EndOfStreamException" does not catch the end of
stream exception and enters an infinite loop!
How can I read the stream, one line at at time, and process
"ALL" the lines in the file?


Nov 20 '05 #5

P: n/a
Thanks for the help!

By changing the "Loop While statement" I was able to get the desired
results.
Changing to:
Loop While NOT currentLine IS Nothing
did the job!
"Doug Gifford" <do*********@cox.net> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
WROX "Beginning VB.Net" 2nd Edition

Using this code from Chapter 11 Page 472:
Do
Try
' Read a line from the file...
currentLine = myReader.ReadLine
lineCounter = lineCounter + 1
Catch e As EndOfStreamException
' Display the message...
MessageBox.Show(e.Message)
' Exit the Do loop...
Exit Do
Finally
' Concatenate the data to the strData
variable...
currentData = currentData & currentLine & vbCrLf End Try
Loop While currentLine <> Nothing

The text file I am reading has blank lines in it.
Sample:

Line one <CrLf>
Line two <CrLf>
<CrLf>
Line Four <CrLf>
Line Five <CrLf>
Line Six <CrLf>

When the code reaches Line Four it exit the loop because
currentLine = Nothing.

I want to read all the lines (one at a time) and exit the loop
when I have reached the end of the file. I still want to read past the blank lines.

If I use a straight loop without the "While currentLine <>
Nothing" the "Catch e As EndOfStreamException" does not catch the end of
stream exception and enters an infinite loop!
How can I read the stream, one line at at time, and process
"ALL" the lines in the file?


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.