Marek
Marek

Reputation: 101

Static functions/subs vs static variables

What would be an underlying difference in the following aspects of the ensuing two subs:

  1. Scope of the subs? (This question primarily applies to the second routine)
  2. Scope of the variable declarations within?
  3. Application of one vs another?

1:

Sub test1()
    Static x As Byte
    x = 1
End Sub

2:

Static Sub test2()
    Dim y As Byte
    y = 2
End Sub

Upvotes: 2

Views: 124

Answers (1)

GSerg
GSerg

Reputation: 78180

Making the entire procedure Static is a shortcut for declaring all its local variables Static.

So there is effectively no difference between the subs, because effectively all their local variables are declared as Static and as such are preserved between the calls.

But as in both subs you manually overwrite the value to something else right after the variable declaration, the effect of preserving the value does nothing for you.

There is no difference in the scope of the subs (it remains what it was before Static), or the the scope of the local variables (they remain local).

Upvotes: 1

Related Questions