Interesting Results In VB.Net

  • Thread starter Thread starter Mythran
  • Start date Start date
M

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
 
Mythran said:
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.
 
Mythran said:
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 said:
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).
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

Thanks for the reply Tom...

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.

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.
 
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
 
Mythran said:
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
 
Mythran said:
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,

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
 
Larry said:
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,

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
 
Cor Ligthert said:
Mythran,

I did not test it however does this build..


The Then is missing


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


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
 
Tom Shelton said:
Add the "Then" to the if statement, and it compiles just fine (well, as
long as you import System.Text).

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
 
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
 
Cor said:
Tom,



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 :)
 
Mythran said:
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...
 
Brian said:
What's wrong with making the scope of variables as narrow as possible?

Brian

C-Services Holland b.v. said:
Eeeuuw.. dimming vars in a loop

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,

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

 
Cor Ligthert said:
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. said:
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.

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
 
Back
Top