Interesting Results In VB.Net

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

    #1

    Interesting Results In VB.Net

    For i As Integer = 0 To 5
    Dim sb As StringBuilder
    If i = 0
    sb = New StringBuilder(i .ToString())
    Next i
    Console.WriteLi ne(sb.ToString( ))
    Next i

    The above code gives the following output:

    0
    0
    0
    0
    0

    Kinda wierd how the StringBuilder (or any object) saves it state after the
    first iteration of the loop. The StringBuilder's text stays the same and
    doesn't re-initialize.

    Comments?

    Mythran

  • creator_bob

    #2
    Re: Interesting Results In VB.Net


    Mythran wrote:[color=blue]
    > For i As Integer = 0 To 5
    > Dim sb As StringBuilder
    > If i = 0
    > sb = New StringBuilder(i .ToString())
    > Next i
    > Console.WriteLi ne(sb.ToString( ))
    > Next i
    >
    > The above code gives the following output:
    >
    > 0
    > 0
    > 0
    > 0
    > 0
    >
    > Kinda wierd how the StringBuilder (or any object) saves it state after the
    > first iteration of the loop. The StringBuilder's text stays the same and
    > doesn't re-initialize.
    >
    > Comments?
    >
    > Mythran[/color]

    Is there any guarantee that short-scoped variables are really
    short-life variables? They may have the same life-time as the
    subroutine or function they are in, depending upon how the compiler
    wishes to compile the code.

    Comment

    • Tom Shelton

      #3
      Re: Interesting Results In VB.Net


      Mythran wrote:[color=blue]
      > For i As Integer = 0 To 5
      > Dim sb As StringBuilder
      > If i = 0
      > sb = New StringBuilder(i .ToString())
      > Next i
      > Console.WriteLi ne(sb.ToString( ))
      > Next i
      >
      > The above code gives the following output:
      >
      > 0
      > 0
      > 0
      > 0
      > 0
      >
      > Kinda wierd how the StringBuilder (or any object) saves it state after the
      > first iteration of the loop. The StringBuilder's text stays the same and
      > doesn't re-initialize.
      >
      > Comments?[/color]

      Not really that suprising... VB.NET supports block scope. So, you
      declare a reference type at the begining of the scope (the scope being
      the for/next):

      Dim sb As StringBuilder

      That reserves storage - but does not initialize the value. You only
      initialize the value once, when i = 0. If you had modified the code to
      look like:

      For i As Integer = 0 To 5
      Dim sb As New StringBuilder
      If i = 0 Then
      sb.Append(i.ToS tring())
      End If
      Console.WriteLi ne(sb)
      Next

      Then you would have gotten very different results.

      --
      Tom Shelton [MVP]

      Comment

      • Mythran

        #4
        Re: Interesting Results In VB.Net


        "Tom Shelton" <tom@mtogden.co m> wrote in message
        news:1145921361 .015189.240390@ j33g2000cwa.goo glegroups.com.. .[color=blue]
        >
        > Mythran wrote:[color=green]
        >> For i As Integer = 0 To 5
        >> Dim sb As StringBuilder
        >> If i = 0
        >> sb = New StringBuilder(i .ToString())
        >> Next i
        >> Console.WriteLi ne(sb.ToString( ))
        >> Next i
        >>
        >> The above code gives the following output:
        >>
        >> 0
        >> 0
        >> 0
        >> 0
        >> 0
        >>
        >> Kinda wierd how the StringBuilder (or any object) saves it state after
        >> the
        >> first iteration of the loop. The StringBuilder's text stays the same and
        >> doesn't re-initialize.
        >>
        >> Comments?[/color]
        >
        > Not really that suprising... VB.NET supports block scope. So, you
        > declare a reference type at the begining of the scope (the scope being
        > the for/next):
        >[/color]
        Aye, not suprising, but still interesting. I have been thinking all along
        that it gets re-dimmed (not ReDim but re-dimensioned) because it would be
        out of scope at the end of each iteration...but I had been mistaken. It
        does NOT get re-dimensioned. It gets dimensioned a single time and seem to
        not even look at it after every iteration after (I haven't check the IL so
        not too sure).
        [color=blue]
        > Dim sb As StringBuilder
        >
        > That reserves storage - but does not initialize the value. You only
        > initialize the value once, when i = 0. If you had modified the code to
        > look like:
        >
        > For i As Integer = 0 To 5
        > Dim sb As New StringBuilder
        > If i = 0 Then
        > sb.Append(i.ToS tring())
        > End If
        > Console.WriteLi ne(sb)
        > Next
        >
        > Then you would have gotten very different results.
        >[/color]
        Yeah, I know. Before my original post, I tested that as well. That wasn't
        interesting though, already knew it heh :P
        [color=blue]
        > --
        > Tom Shelton [MVP]
        >[/color]

        Thanks for the reply Tom...

        Mythran

        Comment

        • david

          #5
          Re: Interesting Results In VB.Net

          On 2006-04-24, Mythran <kip_potter@hot mail.comREMOVET RAIL> wrote:[color=blue]
          > For i As Integer = 0 To 5
          > Dim sb As StringBuilder
          > If i = 0
          > sb = New StringBuilder(i .ToString())
          > Next i
          > Console.WriteLi ne(sb.ToString( ))
          > Next i
          >
          > The above code gives the following output:
          >
          > 0
          > 0
          > 0
          > 0
          > 0
          >
          > Kinda wierd how the StringBuilder (or any object) saves it state after the
          > first iteration of the loop. The StringBuilder's text stays the same and
          > doesn't re-initialize.[/color]

          That's bitten me before, and I scoured the language spec at one point
          for a discussion of it, but couldn't find anything one way or another
          about it. It feels wrong to me, but I'm pretty vague about why it feels
          wrong.

          However, declarations with initializers *are* executed at each iteration
          of the loop.

          For i as Integer = 0 to 5
          Dim sb As StringBuilder = Nothing
          '''

          Since VS2005 pretty much insists on the initializer anyway, that makes
          the issue go away, for me at least.







          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Interesting Results In VB.Net

            Mythran,

            I did not test it however does this build..
            [color=blue]
            > For i As Integer = 0 To 5
            > Dim sb As StringBuilder
            > If i = 0[/color]

            The Then is missing
            [color=blue]
            > sb = New StringBuilder(i .ToString())[/color]

            It is a multiline If so there should be an End IF
            [color=blue]
            > Next i[/color]

            [color=blue]
            > Console.WriteLi ne(sb.ToString( ))
            > Next i[/color]

            There is only one For while there are two "Next" for that.

            Strange in my opinion.

            Cor


            Comment

            • Larry Lard

              #7
              Re: Interesting Results In VB.Net


              Mythran wrote:[color=blue]
              > For i As Integer = 0 To 5
              > Dim sb As StringBuilder
              > If i = 0 '1
              > sb = New StringBuilder(i .ToString()) '1
              > Next i
              > Console.WriteLi ne(sb.ToString( )) '2
              > Next i '3
              >
              > The above code gives the following output:[/color]

              No, the above code does not compile. What did you actually run?

              Errors:
              1: If without End If
              2: 'sb' not declared (because the scope it was in was closed by the
              previous line)
              3: Next without For

              --
              Larry Lard
              Replies to group please

              Comment

              • C-Services Holland b.v.

                #8
                Re: Interesting Results In VB.Net

                Mythran wrote:
                [color=blue]
                > For i As Integer = 0 To 5
                > Dim sb As StringBuilder
                > If i = 0
                > sb = New StringBuilder(i .ToString())
                > Next i
                > Console.WriteLi ne(sb.ToString( ))
                > Next i
                >
                > The above code gives the following output:
                >
                > 0
                > 0
                > 0
                > 0
                > 0
                >
                > Kinda wierd how the StringBuilder (or any object) saves it state after
                > the first iteration of the loop. The StringBuilder's text stays the
                > same and doesn't re-initialize.
                >
                > Comments?
                >
                > Mythran
                >[/color]
                Eeeuuw.. dimming vars in a loop


                --
                Rinze van Huizen
                C-Services Holland b.v

                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: Interesting Results In VB.Net

                  Rinze,

                  [color=blue]
                  > Eeeuuw.. dimming vars in a loop
                  >[/color]
                  As I do forever. Those dims should be in the stack from the loop and with
                  that in my opinion nicely be cleaned up. Strange is that it does not as we
                  have seen.

                  Cor

                  [color=blue]
                  >
                  > --
                  > Rinze van Huizen
                  > C-Services Holland b.v[/color]


                  Comment

                  • Tom Shelton

                    #10
                    Re: Interesting Results In VB.Net


                    Larry Lard wrote:[color=blue]
                    > Mythran wrote:[color=green]
                    > > For i As Integer = 0 To 5
                    > > Dim sb As StringBuilder
                    > > If i = 0 '1
                    > > sb = New StringBuilder(i .ToString()) '1
                    > > Next i
                    > > Console.WriteLi ne(sb.ToString( )) '2
                    > > Next i '3
                    > >
                    > > The above code gives the following output:[/color]
                    >
                    > No, the above code does not compile. What did you actually run?
                    >
                    > Errors:
                    > 1: If without End If
                    > 2: 'sb' not declared (because the scope it was in was closed by the
                    > previous line)
                    > 3: Next without For
                    >[/color]

                    Add the "Then" to the if statement, and it compiles just fine (well, as
                    long as you import System.Text).

                    --
                    Tom Shelton [MVP]

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: Interesting Results In VB.Net

                      Tom,

                      [color=blue]
                      > Add the "Then" to the if statement, and it compiles just fine (well, as
                      > long as you import System.Text).
                      >[/color]

                      Are you sure, one For loop which has two Next and no End If in a multiline
                      If?.

                      Did you try it with Mono?

                      Cor

                      "Tom Shelton" <tom@mtogden.co m> schreef in bericht
                      news:1145976995 .312423.256840@ y43g2000cwc.goo glegroups.com.. .[color=blue]
                      >
                      > Larry Lard wrote:[color=green]
                      >> Mythran wrote:[color=darkred]
                      >> > For i As Integer = 0 To 5
                      >> > Dim sb As StringBuilder
                      >> > If i = 0 '1
                      >> > sb = New StringBuilder(i .ToString()) '1
                      >> > Next i
                      >> > Console.WriteLi ne(sb.ToString( )) '2
                      >> > Next i '3
                      >> >
                      >> > The above code gives the following output:[/color]
                      >>
                      >> No, the above code does not compile. What did you actually run?
                      >>
                      >> Errors:
                      >> 1: If without End If
                      >> 2: 'sb' not declared (because the scope it was in was closed by the
                      >> previous line)
                      >> 3: Next without For
                      >>[/color]
                      >
                      > Add the "Then" to the if statement, and it compiles just fine (well, as
                      > long as you import System.Text).
                      >
                      > --
                      > Tom Shelton [MVP]
                      >[/color]


                      Comment

                      • Mythran

                        #12
                        Re: Interesting Results In VB.Net


                        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                        news:ekEYmSCaGH A.4580@TK2MSFTN GP03.phx.gbl...[color=blue]
                        > Mythran,
                        >
                        > I did not test it however does this build..
                        >[color=green]
                        >> For i As Integer = 0 To 5
                        >> Dim sb As StringBuilder
                        >> If i = 0[/color]
                        >
                        > The Then is missing
                        >[color=green]
                        >> sb = New StringBuilder(i .ToString())[/color]
                        >
                        > It is a multiline If so there should be an End IF
                        >[color=green]
                        >> Next i[/color]
                        >
                        >[color=green]
                        >> Console.WriteLi ne(sb.ToString( ))
                        >> Next i[/color]
                        >
                        > There is only one For while there are two "Next" for that.
                        >
                        > Strange in my opinion.
                        >
                        > Cor
                        >
                        >[/color]

                        lol oops! I didn't even catch it after I re-read it...

                        Anywho, looks like

                        For i As Integer = 0 To 5
                        Dim sb As StringBuilder
                        If i = 0
                        sb = New StringBuilder(i .ToString())
                        End If
                        Console.WriteLi ne(sb.ToString( ))
                        Next i


                        And, on the first point, the "Then" keyword is not required :) I never use
                        it in VB.Net...makes my if's one word shorter..and doesn't look bad either
                        ;)

                        Thanks for pointing that mistake out, :)

                        Mythran

                        Comment

                        • Mythran

                          #13
                          Re: Interesting Results In VB.Net


                          "Tom Shelton" <tom@mtogden.co m> wrote in message
                          news:1145976995 .312423.256840@ y43g2000cwc.goo glegroups.com.. .[color=blue]
                          >
                          > Larry Lard wrote:[color=green]
                          >> Mythran wrote:[color=darkred]
                          >> > For i As Integer = 0 To 5
                          >> > Dim sb As StringBuilder
                          >> > If i = 0 '1
                          >> > sb = New StringBuilder(i .ToString()) '1
                          >> > Next i
                          >> > Console.WriteLi ne(sb.ToString( )) '2
                          >> > Next i '3
                          >> >
                          >> > The above code gives the following output:[/color]
                          >>
                          >> No, the above code does not compile. What did you actually run?
                          >>
                          >> Errors:
                          >> 1: If without End If
                          >> 2: 'sb' not declared (because the scope it was in was closed by the
                          >> previous line)
                          >> 3: Next without For
                          >>[/color]
                          >
                          > Add the "Then" to the if statement, and it compiles just fine (well, as
                          > long as you import System.Text).
                          >
                          > --
                          > Tom Shelton [MVP]
                          >[/color]

                          Grr...

                          1: If DOES NOT REQUIRE a "Then" keyword. Try it and find out ;)
                          2: The code DOES compile if you change the first instance of "Next i" to
                          "End If" .. my mistake on that.
                          3: See #2 for that fix.

                          Sorry, that's what I get for typing off top of head...I hate how OE tries to
                          re-format my code when I copy and paste, so I either open notepad and do 2
                          copies/pastes, or just type off top of head..

                          HTH,
                          Mythran

                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: Interesting Results In VB.Net

                            Mytrhan,

                            [color=blue]
                            > And, on the first point, the "Then" keyword is not required :) I never
                            > use it in VB.Net...makes my if's one word shorter..and doesn't look bad
                            > either ;)
                            >[/color]
                            You should tell this to Herfried, I hate that "then", in the same way as I
                            hate the "Dim". In the current VB there is with Strict and Explicit on not
                            any need for that. But a lot find that nice, some nostalgie as they describe
                            it to me why it is needed.

                            Although for me these keyword could stay optional to keep it backwards
                            compatible.

                            Cor


                            Comment

                            • Tom Shelton

                              #15
                              Re: Interesting Results In VB.Net


                              Cor Ligthert [MVP] wrote:[color=blue]
                              > Tom,
                              >
                              >[color=green]
                              > > Add the "Then" to the if statement, and it compiles just fine (well, as
                              > > long as you import System.Text).
                              > >[/color]
                              >
                              > Are you sure, one For loop which has two Next and no End If in a multiline
                              > If?.[/color]

                              Actually, I goofed. I didn't notice the extra next. It really isn't a
                              proper if statement. I didn't cut and paste his code when I tried it.
                              So, I just typed it the way it should have been :)
                              [color=blue]
                              > Did you try it with Mono?[/color]

                              No. I tried it in VS.NET 2003 :)

                              --
                              Tom Shelton [MVP]

                              Comment

                              Working...