How do I read interspersed blank lines (CrLf) using streams?

L

Larry Woods

I have a seq. file with some blank lines (CrLf only) in it. When the line
is read, I get a value of "Nothing" returned, which give me an "End of File"
condition, so I terminate my procedure.

How do you get around interspersed blank lines?

TIA,

Larry Woods
 
M

Mattias Sjögren

Larry,
I have a seq. file with some blank lines (CrLf only) in it. When the line
is read, I get a value of "Nothing" returned, which give me an "End of File"
condition, so I terminate my procedure.

You probably have code similar to this

Dim line As String = yourStreamReader.ReadLine()
Do Until line = Nothing
...
line = yourStreamReader.ReadLine()
Loop

The problem is that (line = Nothing) evaluates to True for empty
strings as well. Change it to

Do Until line Is Nothing



Mattias
 
L

Larry Woods

You are soooo right. Thanks much. Any possibility you could give me a hint
on where I would have found that in the VB doc? I "thought" that I had
looked everywhere!

TIA,

Larry Woods
 
H

Herfried K. Wagner [MVP]

Hello,

Larry Woods said:
I have a seq. file with some blank lines (CrLf only) in it.
When the line is read, I get a value of "Nothing" returned,
which give me an "End of File" condition, so I terminate
my procedure.

How do you get around interspersed blank lines?

\\\
Imports System.IO
..
..
..
Dim sr As New StreamReader("C:\WINDOWS\WIN.INI")
Dim strLine As String
strLine = sr.ReadLine()
Do Until strLine Is Nothing
MsgBox(strLine)
strLine = sr.ReadLine()
Loop
sr.Close()
///

HTH,
Herfried K. Wagner
 
F

Fergus Cooney

Hi Larry,

Nothing is not just the equivalent of a null pointer. It means the default
value for the type in question.

In the case of an object that will be a null pointer. Strings, however,
while being objects, are treated as a special case.

|| if sSomeString = Nothing

The sSomeString in this context is the string contents and the Nothing is
the default for string contents, ie "".

|| if sSomeStringVar Is Nothing

The sSomeStringVar in this context is the pointer to string contents and
the Nothing is the default for objects which is null pointer.

To have understood this you would have had to have read the sections on
Nothing, Strings, Expressions and a couple of other topics, I'm sure. You may
well have read all these areas, but often an understanding of any one help
topic relies on making connections from other topics, plus a bit of fiddling
about with code, plus a bit of asking other people. Which you've just done.
:)

Regards,
Fergus
 

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