7

Given the code sample below, it seems that the variable currOn is raised up outside of the loop and only instantiated once. For example, say there are three items in itemList and on the second iteration SomeFunctionThatDoesSomeStuff returns true. The value of currOn will then be true. On the third iteration, I would have thought that given VB.NET is a block scope language that currOn would be re-instantiated and default to false; however, I see that it remains true and therefore no matter the value of sOn, does not get updated in further iterations. It seems like javascript's function scope where the declaration of currOn would be pulled up outside the loop. Anyone know what's going on here?

        For Each item As MyItem In itemList
            Dim currOn As Boolean
            Dim sOn As Boolean = SomeFunctionThatDoesStuff(item)
            currOn = currOn OrElse sOn

            Debug.Print(String.Format("the value of currOn is: {0}", currOn))
        Next

As a further example, explicitly setting currOn = false every iteration seems to work as I would have expected the above to work.

            For Each item As MyItem In itemList

                Dim currOn As Boolean = False
                Dim sOn As Boolean = SomeFunctionThatDoesStuff()
                currOn = currOn OrElse sOn

                Debug.Print(String.Format("the value of currOn is: {0}", currOn))
            Next
5
  • 1
    Very interesting, because If you took the code out of a loop and just repeated it manually, you would get a compile error due to duplicate declaration of a variable in the same scope - The fact that the there is only one line must mean the compiler only reads it once and flags it as being 'legal' code, irrespective of the fact the the process will be duplicated. Sep 16, 2015 at 22:07
  • This surprises me too. Probably this is to make sure VB6 code ports better.
    – GSerg
    Sep 16, 2015 at 22:07
  • Interesting...maybe something to do with VB.NET options? Note that the two code samples are not exactly the same - the second one has an inner loop. I'd be curious to see the behavior if you initialize currOn to "False" at the beginning of each loop in the first example.
    – Tim
    Sep 16, 2015 at 22:08
  • 1
    That is very interesting. Seems that the code does not go through the Dim again unless you add an assignment. When the assignment is added, the code does run through the line, but it is not reallocating the variables at the top of the loop, merely resetting their values. Nice find.
    – StarPilot
    Sep 16, 2015 at 22:50
  • @Tim thanks for pointing that out..I actually didn't mean to include the second loop in the second example. The code I found this happening in was much more complicated but I found that it happened with just the simple example. I'll edit it to make that more clear.
    – TKTS
    Sep 17, 2015 at 13:17

1 Answer 1

8

When declaring a variable within a For loop you are declaring it within a block scope. The objects that have been declared within a block will only be accessible within that block but will have a lifetime across the entire procedure.

From MSDN:

Even if the scope of a variable is limited to a block, its lifetime is still that of the entire procedure. If you enter the block more than once during the procedure, each block variable retains its previous value. To avoid unexpected results in such a case, it is wise to initialize block variables at the beginning of the block.

MSDN Link: https://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.