Solved

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

Posted on 2010-08-31
3
598 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
ID: 33572211
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
ID: 33572224
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
ID: 33573070
Thanks for the clarification.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

948 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

21 Experts available now in Live!

Get 1:1 Help Now