Home All Groups Group Topic Archive Search About

Interesting Results In VB.Net

Author
24 Apr 2006 10:56 PM
Mythran
For i As Integer = 0 To 5
    Dim sb As StringBuilder
    If i = 0
        sb = New StringBuilder(i.ToString())
    Next i
    Console.WriteLine(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

Author
24 Apr 2006 11:15 PM
creator_bob
Mythran wrote:
Show quoteHide quote
> For i As Integer = 0 To 5
>     Dim sb As StringBuilder
>     If i = 0
>         sb = New StringBuilder(i.ToString())
>     Next i
>     Console.WriteLine(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

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.
Author
24 Apr 2006 11:29 PM
Tom Shelton
Mythran wrote:
Show quoteHide quote
> For i As Integer = 0 To 5
>     Dim sb As StringBuilder
>     If i = 0
>         sb = New StringBuilder(i.ToString())
>     Next i
>     Console.WriteLine(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?

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.ToString())
            End If
            Console.WriteLine(sb)
   Next

Then you would have gotten very different results.

--
Tom Shelton [MVP]
Author
25 Apr 2006 12:00 AM
Mythran
Show quote Hide quote
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1145921361.015189.240390@j33g2000cwa.googlegroups.com...
>
> Mythran wrote:
>> For i As Integer = 0 To 5
>>     Dim sb As StringBuilder
>>     If i = 0
>>         sb = New StringBuilder(i.ToString())
>>     Next i
>>     Console.WriteLine(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?
>
> 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):
>
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).

Show quoteHide quote
> 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.ToString())
>            End If
>            Console.WriteLine(sb)
>   Next
>
> Then you would have gotten very different results.
>
Yeah, I know.  Before my original post, I tested that as well.  That wasn't
interesting though, already knew it heh :P

> --
> Tom Shelton [MVP]
>

Thanks for the reply Tom...

Mythran
Author
25 Apr 2006 2:33 AM
david
On 2006-04-24, Mythran <kip_potter@hotmail.comREMOVETRAIL> wrote:
Show quoteHide quote
> For i As Integer = 0 To 5
>     Dim sb As StringBuilder
>     If i = 0
>         sb = New StringBuilder(i.ToString())
>     Next i
>     Console.WriteLine(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.

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.
Author
25 Apr 2006 4:58 AM
Cor Ligthert [MVP]
Mythran,

I did not test it however does this build..

> For i As Integer = 0 To 5
>    Dim sb As StringBuilder
>    If i = 0

The Then is missing

>        sb = New StringBuilder(i.ToString())

It is a multiline If so there should be an End IF

>    Next i


>    Console.WriteLine(sb.ToString())
> Next i

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

Strange in my opinion.

Cor
Author
25 Apr 2006 3:59 PM
Mythran
Show quote Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:ekEYmSCaGHA.4580@TK2MSFTNGP03.phx.gbl...
> Mythran,
>
> I did not test it however does this build..
>
>> For i As Integer = 0 To 5
>>    Dim sb As StringBuilder
>>    If i = 0
>
> The Then is missing
>
>>        sb = New StringBuilder(i.ToString())
>
> It is a multiline If so there should be an End IF
>
>>    Next i
>
>
>>    Console.WriteLine(sb.ToString())
>> Next i
>
> There is only one For while there are two "Next" for that.
>
> Strange in my opinion.
>
> Cor
>
>

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.WriteLine(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
Author
25 Apr 2006 4:43 PM
Cor Ligthert [MVP]
Mytrhan,


> 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 ;)
>
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
Author
25 Apr 2006 9:16 AM
Larry Lard
Mythran wrote:
> For i As Integer = 0 To 5
>     Dim sb As StringBuilder
>     If i = 0                 '1
>         sb = New StringBuilder(i.ToString())         '1
>     Next i
>     Console.WriteLine(sb.ToString())                '2
> Next i                                                     '3
>
> The above code gives the following output:

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
Author
25 Apr 2006 2:56 PM
Tom Shelton
Larry Lard wrote:
Show quoteHide quote
> Mythran wrote:
> > For i As Integer = 0 To 5
> >     Dim sb As StringBuilder
> >     If i = 0                 '1
> >         sb = New StringBuilder(i.ToString())         '1
> >     Next i
> >     Console.WriteLine(sb.ToString())                '2
> > Next i                                                     '3
> >
> > The above code gives the following output:
>
> 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
>

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

--
Tom Shelton [MVP]
Author
25 Apr 2006 3:09 PM
Cor Ligthert [MVP]
Tom,


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

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

Show quoteHide quote
"Tom Shelton" <t**@mtogden.com> schreef in bericht
news:1145976995.312423.256840@y43g2000cwc.googlegroups.com...
>
> Larry Lard wrote:
>> Mythran wrote:
>> > For i As Integer = 0 To 5
>> >     Dim sb As StringBuilder
>> >     If i = 0                 '1
>> >         sb = New StringBuilder(i.ToString())         '1
>> >     Next i
>> >     Console.WriteLine(sb.ToString())                '2
>> > Next i                                                     '3
>> >
>> > The above code gives the following output:
>>
>> 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
>>
>
> Add the "Then" to the if statement, and it compiles just fine (well, as
> long as you import System.Text).
>
> --
> Tom Shelton [MVP]
>
Author
25 Apr 2006 5:02 PM
Tom Shelton
Cor Ligthert [MVP] wrote:
> Tom,
>
>
> > Add the "Then" to the if statement, and it compiles just fine (well, as
> > long as you import System.Text).
> >
>
> Are you sure, one For loop which has two Next  and no End If in a multiline
> If?.

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 :)

> Did you try it with Mono?

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

--
Tom Shelton [MVP]
Author
25 Apr 2006 4:02 PM
Mythran
Show quote Hide quote
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1145976995.312423.256840@y43g2000cwc.googlegroups.com...
>
> Larry Lard wrote:
>> Mythran wrote:
>> > For i As Integer = 0 To 5
>> >     Dim sb As StringBuilder
>> >     If i = 0                 '1
>> >         sb = New StringBuilder(i.ToString())         '1
>> >     Next i
>> >     Console.WriteLine(sb.ToString())                '2
>> > Next i                                                     '3
>> >
>> > The above code gives the following output:
>>
>> 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
>>
>
> Add the "Then" to the if statement, and it compiles just fine (well, as
> long as you import System.Text).
>
> --
> Tom Shelton [MVP]
>

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
Author
25 Apr 2006 5:29 PM
Tom Shelton
Mythran wrote:
Show quoteHide quote
> "Tom Shelton" <t**@mtogden.com> wrote in message
> news:1145976995.312423.256840@y43g2000cwc.googlegroups.com...
> >
> > Larry Lard wrote:
> >> Mythran wrote:
> >> > For i As Integer = 0 To 5
> >> >     Dim sb As StringBuilder
> >> >     If i = 0                 '1
> >> >         sb = New StringBuilder(i.ToString())         '1
> >> >     Next i
> >> >     Console.WriteLine(sb.ToString())                '2
> >> > Next i                                                     '3
> >> >
> >> > The above code gives the following output:
> >>
> >> 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
> >>
> >
> > Add the "Then" to the if statement, and it compiles just fine (well, as
> > long as you import System.Text).
> >
> > --
> > Tom Shelton [MVP]
> >
>
> Grr...
>
> 1: If DOES NOT REQUIRE a "Then" keyword.  Try it and find out ;)

Doh!  Your right it is optional in VB.NET.  I guess, I never realized
it because VS just automatically adds it - and it used to be required
in VB5/6.  I'm glad they made it optional...

> 2: The code DOES compile if you change the first instance of "Next i" to
> "End If" .. my mistake on that.

I never noticed it...  I just typed your code into VS - so it just did
it :)

> 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..

It is a pain that...

--
Tom Shelton [MVP]
Author
25 Apr 2006 12:06 PM
C-Services Holland b.v.
Mythran wrote:

Show quoteHide quote
> For i As Integer = 0 To 5
>    Dim sb As StringBuilder
>    If i = 0
>        sb = New StringBuilder(i.ToString())
>    Next i
>    Console.WriteLine(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
>
Eeeuuw.. dimming vars in a loop


--
Rinze van Huizen
C-Services Holland b.v
Author
25 Apr 2006 1:35 PM
Cor Ligthert [MVP]
Rinze,


> Eeeuuw.. dimming vars in a loop
>
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


Show quoteHide quote
>
> --
> Rinze van Huizen
> C-Services Holland b.v
Author
25 Apr 2006 6:08 PM
Brian Gideon
What's wrong with making the scope of variables as narrow as possible?

Brian

C-Services Holland b.v. wrote:
Show quoteHide quote
> Eeeuuw.. dimming vars in a loop
>
>
> --
> Rinze van Huizen
> C-Services Holland b.v
Author
26 Apr 2006 11:50 AM
C-Services Holland b.v.
Brian Gideon wrote:
Show quoteHide quote
> What's wrong with making the scope of variables as narrow as possible?
>
> Brian
>
> C-Services Holland b.v. wrote:
>
>>Eeeuuw.. dimming vars in a loop
>>
>>
>>--
>>Rinze van Huizen
>>C-Services Holland b.v
>
>

I just don't like dimming variables all over the place. It's a sure fire
  way to lose one. I don't care if it works, I just find it plain ugly.


--
Rinze van Huizen
C-Services Holland b.v
Author
26 Apr 2006 12:31 PM
Cor Ligthert [MVP]
Rinze,

You should try it, beside more efficient is it even quicker when you are
making a program.

Declaring global variables in the Main Section is something from the Cobol
time and very inefficient.

Cor

Show quoteHide quote
"C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht
news:8PadnYAIxYH7w9LZnZ2dnUVZ8tydnZ2d@zeelandnet.nl...
> Brian Gideon wrote:
>> What's wrong with making the scope of variables as narrow as possible?
>>
>> Brian
>>
>> C-Services Holland b.v. wrote:
>>
>>>Eeeuuw.. dimming vars in a loop
>>>
>>>
>>>--
>>>Rinze van Huizen
>>>C-Services Holland b.v
>>
>>
>
> I just don't like dimming variables all over the place. It's a sure fire
> way to lose one. I don't care if it works, I just find it plain ugly.
>
>
> --
> Rinze van Huizen
> C-Services Holland b.v
Author
26 Apr 2006 4:25 PM
Mythran
Show quote Hide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:e5bJO0SaGHA.4248@TK2MSFTNGP03.phx.gbl...
> Rinze,
>
> You should try it, beside more efficient is it even quicker when you are
> making a program.
>
> Declaring global variables in the Main Section is something from the Cobol
> time and very inefficient.
>
> Cor
>
> "C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht
> news:8PadnYAIxYH7w9LZnZ2dnUVZ8tydnZ2d@zeelandnet.nl...
>> Brian Gideon wrote:
>>> What's wrong with making the scope of variables as narrow as possible?
>>>
>>> Brian
>>>
>>> C-Services Holland b.v. wrote:
>>>
>>>>Eeeuuw.. dimming vars in a loop
>>>>
>>>>
>>>>--
>>>>Rinze van Huizen
>>>>C-Services Holland b.v
>>>
>>>
>>
>> I just don't like dimming variables all over the place. It's a sure fire
>> way to lose one. I don't care if it works, I just find it plain ugly.
>>
>>
>> --
>> Rinze van Huizen
>> C-Services Holland b.v
>
>

I used to be the same way, only dimensioning my vars at the top of the
method block...but now, after I tried it and used it...it isn't as ugly as I
thought it to be.  Just dimension at the first point you need to use it.  I
usually try to dimension and initialize at the same time, that way, if I
don't need the var, it's easy to track down :)

HTH,
Mythran
Author
26 Apr 2006 8:23 PM
Tom Shelton
Mythran wrote:
Show quoteHide quote
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:e5bJO0SaGHA.4248@TK2MSFTNGP03.phx.gbl...
> > Rinze,
> >
> > You should try it, beside more efficient is it even quicker when you are
> > making a program.
> >
> > Declaring global variables in the Main Section is something from the Cobol
> > time and very inefficient.
> >
> > Cor
> >
> > "C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht
> > news:8PadnYAIxYH7w9LZnZ2dnUVZ8tydnZ2d@zeelandnet.nl...
> >> Brian Gideon wrote:
> >>> What's wrong with making the scope of variables as narrow as possible?
> >>>
> >>> Brian
> >>>
> >>> C-Services Holland b.v. wrote:
> >>>
> >>>>Eeeuuw.. dimming vars in a loop
> >>>>
> >>>>
> >>>>--
> >>>>Rinze van Huizen
> >>>>C-Services Holland b.v
> >>>
> >>>
> >>
> >> I just don't like dimming variables all over the place. It's a sure fire
> >> way to lose one. I don't care if it works, I just find it plain ugly.
> >>
> >>
> >> --
> >> Rinze van Huizen
> >> C-Services Holland b.v
> >
> >
>
> I used to be the same way, only dimensioning my vars at the top of the
> method block...but now, after I tried it and used it...it isn't as ugly as I
> thought it to be.  Just dimension at the first point you need to use it.  I
> usually try to dimension and initialize at the same time, that way, if I
> don't need the var, it's easy to track down :)
>
> HTH,
> Mythran

I pretty much try to dim my vars in one place - at the begining of the
scope they are used.  But, I also try to limit the scope to as small as
possible.  So - My code might look like (well, I mostly code C#, but
since this is a VB group :)

Public Sub TheBestSubEver ()
    Dim anInteger As Integer
    Dim aString As String

    anInteger = SomeValue

    For i As Integer = 0 To SomeLargeIntegerValue
         Dim anIntegerInThisScope As Integer
         ' Do really cool loopy stuff with i and anIntegerInThisScope
    Next

    aString = "Hurrray, were done!"
End Sub

Anyway...  That's what I do :)

--
Tom Shelton
Author
27 Apr 2006 12:30 PM
C-Services Holland b.v.
Cor Ligthert [MVP] wrote:
> Rinze,
>
> You should try it, beside more efficient is it even quicker when you are
> making a program.
>
> Declaring global variables in the Main Section is something from the Cobol
> time and very inefficient.
>
> Cor
>

Why do you assume I'm declaring everything global in main?  I only
declare vars global when it's absolutely nescesary. Normally I declare
every var I need in a function/sub at the top of that particular
function or sub. I really don't see any advantage to declaring a
variable halfway in a sub in a loop. This has always been my little pet
peeve with Basic (or Clipper for that matter), plucking vars out of the
air when you need them.


--
Rinze van Huizen
C-Services Holland b.v
Author
27 Apr 2006 2:55 PM
Cor Ligthert [MVP]
Rinze,

Feel free to do it your way.

As I always say that there is seldom a best method in VB.

As Mythran I Use

For I as integer = 0 to end
Next.

But as you insist on
Dim I as integer
bla
bla
bla
bla
bla
for I = 0 to end
next

Than that is your own decission

Cor

Show quoteHide quote
"C-Services Holland b.v." <c**@REMOVEcsh4u.nl> schreef in bericht
news:5uidnd7b1M7WJM3ZnZ2dnUVZ8qOdnZ2d@zeelandnet.nl...
> Cor Ligthert [MVP] wrote:
>> Rinze,
>>
>> You should try it, beside more efficient is it even quicker when you are
>> making a program.
>>
>> Declaring global variables in the Main Section is something from the
>> Cobol time and very inefficient.
>>
>> Cor
>>
>
> Why do you assume I'm declaring everything global in main?  I only declare
> vars global when it's absolutely nescesary. Normally I declare every var I
> need in a function/sub at the top of that particular function or sub. I
> really don't see any advantage to declaring a variable halfway in a sub in
> a loop. This has always been my little pet peeve with Basic (or Clipper
> for that matter), plucking vars out of the air when you need them.
>
>
> --
> Rinze van Huizen
> C-Services Holland b.v
Author
27 Apr 2006 1:41 AM
Brian Gideon
C-Services Holland b.v. wrote:
> Brian Gideon wrote:
> > What's wrong with making the scope of variables as narrow as possible?
> >
> I just don't like dimming variables all over the place. It's a sure fire
>   way to lose one. I don't care if it works, I just find it plain ugly.
>

It may seem ugly to you, but to others who have to maintain the code it
is a little more difficult to decipher when and where it is used.  It's
pretty much the defacto standard.

Brian