locked
Default Boolean Value RRS feed

  • Question

  • I am using a boolean embedded in a for next loop. I thought the value would ALWAYS default to false inside this loop, but it seems to hold the value of true inside the loop, even when moving to another value of i. What's up with this?

    For i as integer = 0 to 5
       Dim b as boolean
       .... use boolean value later...
    Next i

    Wednesday, September 7, 2011 12:47 AM

Answers

  • Fascinating. I completely thought that because it Re-Dim'd the variable, it would reset it to the default. Can't believe I missed that one. It really is doing a Redim Preserve.

    Hi Derek,

    It seems that on setting a breakpoint with

    Dim b As Boolean

    is setting up the variable 'b' once within a loop.

    However if you do this:>>

     

    Dim b As Boolean = New Boolean

    it forces b to be declared again in each iteration of the loop.

    I hope this helps.  :)

    Try out this example which creates 6 Buttons but I only have

    Dim btn As Button = New Button

    in the code once.

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.Location = New Point(5, 5)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To 5
    
                Dim b As Boolean = New Boolean
                Dim btn As Button = New Button
    
                btn.Size = New Size(100, 20)
    
                With btn
                    btn.Text = i.ToString
                    btn.Location = New Point(150, i * (btn.Height + 10))
                End With
                Me.Controls.Add(btn)
    
            Next
    
        End Sub
    
    End Class
    
     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    • Proposed as answer by DiegoCattaruzza Wednesday, September 7, 2011 6:29 AM
    • Marked as answer by Kee Poppy Tuesday, September 13, 2011 7:32 AM
    Wednesday, September 7, 2011 2:44 AM

All replies

  • b is a local variable like other variables outside the loop. b is just not visible outside the loop.

    The following two methods are functionally absolutely equal:

     

       Sub test1()
    
          For i = 0 To 5
             Dim var As Boolean
             var = True
          Next
    
          For i = 0 To 5
             Dim var As Integer
             var = 17
          Next
    
       End Sub
    
       Sub test2()
    
          Dim var1 As Boolean
          Dim var2 As Integer
    
          For i = 0 To 5
             var1 = True
          Next
    
          For i = 0 To 5
             var2 = 17
          Next
    
       End Sub
    



    Armin

    Wednesday, September 7, 2011 12:51 AM
  • I am using a boolean embedded in a for next loop. I thought the value would ALWAYS default to false inside this loop, but it seems to hold the value of true inside the loop, even when moving to another value of i. What's up with this?

    For i as integer = 0 to 5
       Dim b as boolean
       .... use boolean value later...
    Next i

    Hi Derek,

    The value of b as a Boolean can be whatever you decide to make it inside your loop.

    Try this code with one Button on a Form and note the results please.

    The variable b will only be accessible within your loop.

     

     

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To 5
                Dim b As Boolean
    
                If i Mod 2 = 0 Then
                    b = True
                Else
                    b = False
                End If
    
                MessageBox.Show("i = " & i.ToString & "     b = " & b.ToString)
    
            Next i
    
        End Sub
    
    End Class
    

     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    Wednesday, September 7, 2011 1:13 AM
  • I am using a boolean embedded in a for next loop. I thought the value would ALWAYS default to false inside this loop, but it seems to hold the value of true inside the loop, even when moving to another value of i. What's up with this?

    For i as integer = 0 to 5
       Dim b as boolean
       .... use boolean value later...
    Next i

    Hi Derek,

    That will only happen if you do not change it from FALSE to TRUE as in this code.>>

     

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To 5
                Dim b As Boolean
    
                MessageBox.Show("i = " & i.ToString & "     b = " & b.ToString)
    
            Next i
    
        End Sub
    
    End Class
    




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    Wednesday, September 7, 2011 1:22 AM
  • Fascinating. I completely thought that because it Re-Dim'd the variable, it would reset it to the default. Can't believe I missed that one. It really is doing a Redim Preserve.
    Wednesday, September 7, 2011 1:34 AM
  • Well, ReDim out of an array context doesn't make much sense. :-) But I guess I know what you mean.
    You just have to be aware of where the variable is located: There's only the stack or the heap. Locals are on the stack. Consequently they don't lose their value until the function returns.

    EDIT:
    A declaration is not an executable statement. It's just there to make the compiler generate the code to reserve space for locals at function entry. You see it if you single-step through the code: The declaration line is not executed.
    I'm already hearing someone saying that, for example, "Dim x(5) as integer" is an executable statment. Yes, but that's just a short for declaration + initilization in one line (actually Dim x() As Integer <CR> Redim x(5) ). Executable is only the initilization.


    Armin



    Wednesday, September 7, 2011 1:44 AM
  • Hi Armin,

            'Dim x(5) As Integer
            'Could be doing:>>
    
            Dim x() As Integer = CType(Array.CreateInstance(GetType(Integer), 6), Integer())
    
            'Not neccessarily:>
            ' Dim x() As Integer
            ' ReDim x(5)
    




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    Wednesday, September 7, 2011 2:12 AM
  • Fascinating. I completely thought that because it Re-Dim'd the variable, it would reset it to the default. Can't believe I missed that one. It really is doing a Redim Preserve.

    Hi Derek,

    It seems that on setting a breakpoint with

    Dim b As Boolean

    is setting up the variable 'b' once within a loop.

    However if you do this:>>

     

    Dim b As Boolean = New Boolean

    it forces b to be declared again in each iteration of the loop.

    I hope this helps.  :)

    Try out this example which creates 6 Buttons but I only have

    Dim btn As Button = New Button

    in the code once.

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Button1.Location = New Point(5, 5)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To 5
    
                Dim b As Boolean = New Boolean
                Dim btn As Button = New Button
    
                btn.Size = New Size(100, 20)
    
                With btn
                    btn.Text = i.ToString
                    btn.Location = New Point(150, i * (btn.Height + 10))
                End With
                Me.Controls.Add(btn)
    
            Next
    
        End Sub
    
    End Class
    
     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    • Proposed as answer by DiegoCattaruzza Wednesday, September 7, 2011 6:29 AM
    • Marked as answer by Kee Poppy Tuesday, September 13, 2011 7:32 AM
    Wednesday, September 7, 2011 2:44 AM
  • You are showing another way of creating an array. I understand, but did I miss the point?
    Armin
    Wednesday, September 7, 2011 2:48 AM
  • Well, ReDim out of an array context doesn't make much sense. :-) But I guess I know what you mean.
    You just have to be aware of where the variable is located: There's only the stack or the heap. Locals are on the stack. Consequently they don't lose their value until the function returns.

    EDIT:
    A declaration is not an executable statement. It's just there to make the compiler generate the code to reserve space for locals at function entry. You see it if you single-step through the code: The declaration line is not executed.
    I'm already hearing someone saying that, for example, "Dim x(5) as integer" is an executable statment. Yes, but that's just a short for declaration + initilization in one line (actually Dim x() As Integer <CR> Redim x(5) ). Executable is only the initilization.


    Armin



    Hi Armin,

    That depends on whether the declaration contains the keyword NEW or not.

    If the declaration contains an = sign it looks like it is executed too, as in this loop:>>

            For i As Integer = 0 To 5
    
                Dim b As Boolean = False
    
            Next
    


    Try the above with a breakpoint on the loop and then remove the

    = False

    Please try my code in the previous post.  :)



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    • Proposed as answer by DiegoCattaruzza Wednesday, September 7, 2011 6:28 AM
    Wednesday, September 7, 2011 2:53 AM
  •     "That depends on whether the declaration contains the keyword NEW or not."

    If it contains a new keyword, the logical line contains an initlization. The declaration itself is still not executable, only the initilization.

         dim bla as new form

    is just a short for

        dim bla as form

        bla = new form

    If you execute the latter, you see that the declartion is (still) not executable.

     

         "If the declaration contains an = sign it looks like it is executed too, as in this loop:"

    Yes, but executable is the assignment, not the declaration.

     

     


    Armin
    Wednesday, September 7, 2011 3:00 AM
  • Hi Armin,

    Agreed.  :-)

    Yes but something still happens in a declaration like memory reserved or whatever.

    ( The memory pointer is set to NOTHING. ) Otherwise you would not be able to execute the assignment.  :)

     

    Only after the assignment is the pointer set to wherever the value type or reference type is in memory.



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.
    Wednesday, September 7, 2011 3:14 AM
  • Hi John,

    Yes but something still happens in a declaration like memory reserved or whatever.

    ( The memory pointer is set to NOTHING. ) Otherwise you would not be able to execute the assignment.  :)

    Sure, the declaration makes something happen, i.e. makes the compiler generate the code for reserving stack space (as I wrote in my first message), though the declaration line is not an executable statement (as you see if you single-step).

    For example, you can put the declaration later in the code:

    1: Dim x as integer
    2: x = 47
    3: Dim y as integer
    4: y = 12

    Stack space is always allocated at function entry. This is true for both variable x and y. If "dim y as integer" was an executable statement, the order of execution would be line 1, 3, 2, 4, but that does not happen.


    Armin



    • Edited by Armin Zingler Wednesday, September 7, 2011 3:59 AM was -> would be
    Wednesday, September 7, 2011 3:56 AM