CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: initialize static variables in a sub

  1. #1
    Join Date
    Apr 2002
    Posts
    388

    initialize static variables in a sub

    Hallo all!

    I have a problem.

    I have a sub with a static boolean variable. The first time the sub is called the variable should be initialized with true.

    How could this be achived?

    It would be very nice if anyone could help me!
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  2. #2
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569
    Just set it to true as the first line of the subroutine...

    UNLESS you mean it must be set to true initially but not after that.
    then...

    have another boolean variable e.g. "hasBeenRun" which is false until the subroutine is run then it is set to true. Only if "hasBeenRun" is false is your first variable set to true, thereafter leave it allown.
    Mike

  3. #3
    Join Date
    Apr 2002
    Posts
    388
    Case 1: HasBeenRun is global
    That was also a thought of me but in this case I could also make my static variable global.

    Case 2: HasBeenRun is a static variable in the same sub
    Who could tell me that it always be initialized with false?
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  4. #4
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569
    VB initialises Boolean to false.
    Mike

  5. #5
    Join Date
    Apr 2002
    Posts
    388
    Is this really defined?
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  6. #6
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054
    Why not just make it a modular level variable and initialize at FormLoad? Static and Modular variables are just same after all (created on class initialization).
    Marketing our skills - please participate in the survey and share your insights
    -

  7. #7
    Join Date
    Apr 2002
    Posts
    388
    Yes, I also thought at it, but it is not a very nice stil of programming. What do I have static variable for if I am not able to initialize them?

    Perhaps someone could help me in this case...
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  8. #8
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569
    Yes, in VB boolean is initialized to FALSE....

    ALWAYS !
    Mike

  9. #9
    Join Date
    Apr 2002
    Posts
    388
    Thank you for your reply!
    I'll give it a try!
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  10. #10
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054
    Normally, this is what I do when using a static. Because Boolean is initialized as False, there must be a way to immediately check that status.
    Code:
    Option Explicit
    
    Sub UseOfStatic()
        Static X As Boolean
        
        If X Then
            '   do something if X is True
            '   ----
        Else
            '   do something if X is false
            '   ----
        End If
    End Sub
    Another option. Ignoring the Sub instructions if the static var remains on its initial status.
    Code:
    Option Explicit
    
    Sub UseOfStatic()
        Static X As Boolean
        
        If X Then
            '   do something if X is True
            '   ----
        End If
    End Sub
    Marketing our skills - please participate in the survey and share your insights
    -

  11. #11
    Join Date
    Apr 2002
    Posts
    388
    Congretly I use it like that:

    Code:
    private sub Timer1_Timer()
        Static WaitOnce As Boolean
        Static InThisFunctionNotForTheFirstTime As Boolean 'initialized with false
        
        'if first time in the function
        If InThisFunctionNotForTheFirstTime = False Then
            WaitOnce = True
            InThisFunctionNotForTheFirstTime = True
        End If
        
        'check if to wait once
        If WaitOnce Then
            WaitOnce = False
        Else
            '--- do something
            
            'check if to wait once
            waitonce = iif (somecriteria, true, false)
        end if
    end sub
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  12. #12
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Hallo all!

    I have a problem.

    I have a sub with a static boolean variable. The first time the sub is called the variable should be initialized with true.

    How could this be achived?

    It would be very nice if anyone could help me!
    Only vb initializes static variables. But I think this will do too..

    Public Sub MyProcecedure(Option ByVal st_initVal As Variant)
    Static bst_Variable As Boolean

    If Not IsMissing(st_initVal) Then
    bst_Variable = st_initVal
    Exit Sub
    End If

    'codes here

    End Sub
    Busy

  13. #13
    Join Date
    Apr 2002
    Posts
    388
    Yes, but in this case I have to care outside if I call the function for the first time. But I am not able to care, because it is an event triggered by a timer.

    But thank you all for your suggestions!
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

  14. #14
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Ungi,

    No problem..Just transfer the code then spawn this sub routine in the event..
    Privatre Sub Timer1_Timer()

    MyProcedure

    End Sub
    Busy

  15. #15
    Join Date
    Apr 2002
    Posts
    388
    And how should I set the parameter that indicates that it is the first call?
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)