Solved

Difference between an array defined with "Static" keyword and "Dim" keyword?

Posted on 2010-08-31
3
597 Views
Last Modified: 2012-05-10
hi,

Is there any difference between the following two statements?

Static strarray() As String
Dim strarray() As String

I tried to change Dim strarray() As String to Static strarray() As String in the below code. I dont see the difference this way. Can anybody explain? Thanks
       


Private Sub Form_Load()
    Dim strarray() As String
        
    ReDim strarray(2) As String
    
    strarray(0) = "1"
    strarray(1) = "2"
           
    For i = 0 To UBound(strarray) - 1
        MsgBox (strarray(i))
    Next
End Sub

Open in new window

0
Comment
Question by:ipjyo
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
thoffman earned 500 total points
Comment Utility
Static causes the variable to keep the value between function calls. It basically "remembers" the value of the array when the function exits instead of resetting it each time it's called. I've modified the function to show how it would behave (I changed the variable to a long to make it easier to see).
 
If you keep it as dim, the code will return the following every time it's run:
1
2

On the other hand, if you make it static, the first time it's run, it returns:
1
2

Then, the second time it's run, since strarray(0) is set to 1, it returns:
2
3

And so on...

Private Sub Form_Load()

    Dim strarray() As long

        

    ReDim strarray(2) As long

    

    strarray(0) = strarray(0) + 1

    strarray(1) = strarray(0) + 1

           

    For i = 0 To UBound(strarray) - 1

        MsgBox (strarray(i))

    Next

End Sub

Open in new window

0
 
LVL 8

Expert Comment

by:thoffman
Comment Utility
By the way, I just realized that you had that in a Form_Load, so static wouldn't make much sense since the function is only called once anyway. It would be better in a button click function.
0
 

Author Comment

by:ipjyo
Comment Utility
Thanks for the clarification.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now