Loop Condition Question

E

eBob.com

I have the following loop in my simple program ...

Do
line = sr.ReadLine()
If Not line Is Nothing Then
linecount = linecount + 1
If line.Length > 0 Then
If line.Chars(0) = "*"c Then
sw.WriteLine(line)
End If
End If
End If
Loop Until line Is Nothing

It works (unless I made a mistake in removing the comments which I did
to avoid line wrap problems).

But I'd rather do something like this

Do While Not (line = sr.ReadLine) Is Nothing
linecount = linecount + 1
If line.Length > 0 Then
If line.Chars(0) = "*"c Then
sw.WriteLine(line)
End If
End If
Loop


But, given the syntax I've used above, the compiler complains about
"(line = sr.ReadLine) " saying "'Is' requires operands that have
reference types, but this operand has the value type 'Boolean'."

Is there a way to do what I am trying to do? Or another way to avoid
checking for the end of file condition twice? I am trying to avoid
use of GoTo. Also, my emphasis is on performance and simplicity, not
elegance.

Thanks, Bob
 
H

Herfried K. Wagner [MVP]

eBob.com said:
It works (unless I made a mistake in removing the comments which I did
to avoid line wrap problems).

But I'd rather do something like this

Do While Not (line = sr.ReadLine) Is Nothing

This sort of syntax is not supported. Because of VB.NET's overloading of
the '=' operator as assignment and comparison operator assignments are not
allowed in the loop's condition.
Is there a way to do what I am trying to do? Or another way to avoid
checking for the end of file condition twice? I am trying to avoid
use of GoTo.

Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>
 
C

Cor Ligthert

Herfried,

I wanted to be sure that you did not find a solution.
You mean that there should be for *assignment* in this cases something as an
== statement. That would be logical in my opinion. You know something extra
only in this situation.

:)

Cor
 
E

eBob.com

Thank you Herfried. And Wow! An answer on a Sunday and within minutes of
when I posted my question! I think that I will use the StreamReaeder.Peek
method which I learned about in the reference you included.

Thanks, Bob
 

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