Let's look at the following code:
Sub Main()The variable j leaves scope at the end of each iteration, but this code does not print 10 lines containing "1". It counts from 1 to 10.
For i As Integer = 1 To 10
Dim j As Integer
j += 1
Console.WriteLine(j)
Next
End Sub
Why? To save the time of reallocating and reinitializing the variable each loop, the Visual Basic compiler switches the code around a bit so you get this:
Sub Main()The solution is that any variable that you declare inside of your loop needs to be explicitly initialized, like so:
Dim j As Integer
For i As Integer = 1 To 10
j += 1
Console.WriteLine(j)
Next
End Sub
Sub Main()The above snippet will work as expected: 10 lines, each containing a "1".
For i As Integer = 1 To 10
Dim j As Integer = 0
j += 1
Console.WriteLine(j)
Next
End Sub
The worst kind of "gotcha" is an undocumented "gotcha..."
1 comment:
Michael,
IIRC, your first example does exactly what the language specification says it is supposed to do. The only variable that is scoped within the For...Next loop is i. Any variable declarations within a looping structure are pulled out of the loop and declared before the loop; just what the compiler did in your first example. It is bad code to declare variables within a looping structure that the compiler "fixes" for you.
Post a Comment