Loop Condition Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eBob.com

    Loop Condition Question

    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(li ne)
    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(li ne)
    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
  • Herfried K. Wagner [MVP]

    #2
    Re: Loop Condition Question

    "eBob.com" <eBob.com@total lyfakeisp.com> schrieb:[color=blue]
    > 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[/color]

    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.
    [color=blue]
    > 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.[/color]

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

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • Cor Ligthert

      #3
      Re: Loop Condition Question

      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


      Comment

      • eBob.com

        #4
        Re: Loop Condition Question (THANKS, WOW! ...)

        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.P eek
        method which I learned about in the reference you included.

        Thanks, Bob


        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        news:uIi6HZdEFH A.228@tk2msftng p13.phx.gbl...[color=blue]
        > "eBob.com" <eBob.com@total lyfakeisp.com> schrieb:[color=green]
        > > 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[/color]
        >
        > 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.
        >[color=green]
        > > 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.[/color]
        >
        > Reading a text file line-by-line or blockwise with a progress indicator
        > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&la ng=en>
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
        >[/color]


        Comment

        Working...