locked
Dim of a variable inside a for loop RRS feed

  • Question

  • Using VB.NET .NetFramwork 3.5

    This fails and strikes me as odd.  Is this correct behavior:

        Public Sub TestForEach()
            For index As Integer = 1 To 10
                Dim z As Integer
                Assert.AreEqual(0, z)
                z = index
            Next
        End Sub

    I would expect z to always equal 0 as I thought the dim-ing of an integer would set the value to 0.

    If you run this.  It fails at the second loop.  For some reason z retains it's value of 1.

    I know I can manually initialize z to = 0, but I wanted to know if this is a bug or expected behavior.

    Thanks

    Tuesday, March 30, 2010 3:38 PM

Answers

  • It's Expected Behavior , z is declared in For -Next code Block ,it is dim -ed only once  and the default value is set to zero.

     If you  declare a variable inside a loop doesn't mean the previous variable will go out of scope and a new z will be declared with each iteration.

    If you set the value of  z   as you set z = Index the value will be carried to the next loop.

    IF you set  Dim z As Integer = 0  then z will use the value 0 , that is the same As assigning 0 to z  ,z = 0


    Asgar
    • Edited by _asgar Tuesday, March 30, 2010 5:18 PM
    • Proposed as answer by Rudedog2 Tuesday, March 30, 2010 5:47 PM
    • Marked as answer by Shirozmk Tuesday, March 30, 2010 7:45 PM
    Tuesday, March 30, 2010 5:17 PM

All replies

  • I have found your problem.

     

     Assert.AreEqual(0, z)
    

     

    Team System

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, March 30, 2010 3:51 PM
  • It's Expected Behavior , z is declared in For -Next code Block ,it is dim -ed only once  and the default value is set to zero.

     If you  declare a variable inside a loop doesn't mean the previous variable will go out of scope and a new z will be declared with each iteration.

    If you set the value of  z   as you set z = Index the value will be carried to the next loop.

    IF you set  Dim z As Integer = 0  then z will use the value 0 , that is the same As assigning 0 to z  ,z = 0


    Asgar
    • Edited by _asgar Tuesday, March 30, 2010 5:18 PM
    • Proposed as answer by Rudedog2 Tuesday, March 30, 2010 5:47 PM
    • Marked as answer by Shirozmk Tuesday, March 30, 2010 7:45 PM
    Tuesday, March 30, 2010 5:17 PM
  •     Private Sub Button1_Click2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For index As Integer = 1 To 10
                Dim z As Integer = 0 'set the variable explicitly
                Assert.AreEqual(0, z)
                z = index
            Next
        End Sub

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, March 30, 2010 5:48 PM
  • Counting on implicit behavior of a language will drive you nuts.  Explicitly define variables where they are used.  For some reason, this isn't the trend.  The trend is for more implicit behaviour, even for the simplest cases.
    Tuesday, March 30, 2010 5:57 PM
  • Thanks for your reply.

    I've been programming for the past 15 years and I always assumed that hitting the next command made all the variables declared within the loop out of scope and therefore was re-created.  I guess I was wrong.  I feel like I someone just changed the rules on me.

    Thanks for clarifing.

     

     

    Tuesday, March 30, 2010 6:20 PM
  • Thanks for your reply.

    I've been programming for the past 15 years and I always assumed that hitting the next command made all the variables declared within the loop out of scope and therefore was re-created.  I guess I was wrong.  I feel like I someone just changed the rules on me.

    Thanks for clarifing.

     

     

    Tuesday, March 30, 2010 6:20 PM
  • The moral of the story is what John hinted at.  Be explicit, always. 

    Shortcuts in the source code don't pay off as much as one might think.


    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, March 30, 2010 6:41 PM
  •   I feel like I someone just changed the rules on me.


    Only partly.  Try this form:

            For index As Integer = 1 To 10
                Dim z As Object = 0
                MsgBox(z.ToString)
                z += 1
            Next
    

    and its variation

            Dim z As Object = 0
            For index As Integer = 1 To 10
                MsgBox(z.ToString)
                z += 1
            Next
    

     

     

     

    Tuesday, March 30, 2010 9:45 PM
  • Only partly.  Try this form:
            For index As Integer = 1 To 10
                Dim z As Object = 0
                MsgBox(z.ToString)
                z += 1
            Next
            Console.WriteLine(z.ToString)
    

    and its variation

            Dim z As Object = 0
            For index As Integer = 1 To 10
                MsgBox(z.ToString)
                z += 1
            Next
            Console.WriteLine(z.ToString)
    
    
    Tuesday, March 30, 2010 10:09 PM