none
Using a STATIC variable to switch between states. RRS feed

  • Question

  • Hello,

    Can someone please explain the following code?  I was expecting SWITCH to take on a value of FALSE on first run, but it takes on a value of TRUE.  How do I see what the actual value of SWITCH is besides testing it by "printing" the variable?  What does "Not Switch" set SWITCH to?  Please explain the logic, how the variable initializes, and how the code compiles.

    I have assumed  "If switch Then" actually means, "If switch = "True" then".

    'CODE:

    'Toggles the variable switch between two states
    Sub ToggleSwitch()

        Static switch As Boolean

        switch = Not switch

        If switch Then
            MsgBox "on"
        Else
            MsgBox "Off"
        End If

    End Sub

    'Thank you!

    Tuesday, October 30, 2012 3:33 PM

Answers

  • I think I may have figured it out, with a little help from a coworker.  When I declare Static switch As Boolean, it gets initialized as FALSE.  Then the rest makes sense...
    • Marked as answer by itsme82 Tuesday, October 30, 2012 4:58 PM
    • Unmarked as answer by itsme82 Tuesday, October 30, 2012 4:58 PM
    • Marked as answer by itsme82 Wednesday, October 31, 2012 11:57 AM
    Tuesday, October 30, 2012 3:45 PM

All replies

  • I think I may have figured it out, with a little help from a coworker.  When I declare Static switch As Boolean, it gets initialized as FALSE.  Then the rest makes sense...
    • Marked as answer by itsme82 Tuesday, October 30, 2012 4:58 PM
    • Unmarked as answer by itsme82 Tuesday, October 30, 2012 4:58 PM
    • Marked as answer by itsme82 Wednesday, October 31, 2012 11:57 AM
    Tuesday, October 30, 2012 3:45 PM
  • Boolean is initialized to FALSE

    Long or any numeric type is initialized to 0

    String type initilized to zero length string.


    Best Regards,
    Asadulla Javed, Kolkata
    ---------------------------------------------------------------------------------------------
    Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it solves the issue.

    Wednesday, October 31, 2012 7:43 AM
    Answerer
  • Just a little additional remark
     
    If you use a variable named Switch in Access you will run in troubles.
    Switch() is a reserved Word and stands for the Switch Function.
     
    Better is to use some naming conventions to prefix your variables, in this
    case it would make sense to call it
     
    blnSwitch (bln for Boolean)
     
    or staBlnSwitch (sta for static, Bln for Boolean)
     
    "itsme82" schrieb im Newsbeitrag
    news:1f488eac-1757-44e6-9be3-caa56ae973c2@communitybridge.codeplex.com...
    > Can someone please explain the following code?  I was expecting SWITCH to
    > take on a value of FALSE
    > on first run, but it takes on a value of TRUE.  How do I see what the
    > actual value of SWITCH is besides
    > testing it by "printing" the variable?  What does "Not Switch" set SWITCH
    > to?  Please explain the logic,
    > how the variable initializes, and how the code compiles.
     
    I have assumed  "If switch Then" actually means, "If switch = "True" then".
     
    'CODE:
     
    'Toggles the variable switch between two states
    Sub ToggleSwitch()
        Static switch As Boolean
        switch = Not switch
        If switch Then
           MsgBox "on"
       Else
           MsgBox "Off"
       End If
     
    End Sub
     
    'Thank you!
     
     
    Wednesday, October 31, 2012 10:15 AM