Visual Basic – Why Local Variables Can Be ‘Static’ Not ‘Shared’

keywordsstaticvisual basic

In Visual Basic .NET, I can see different keyword for the same(?) concept:

• methods                 Shared
• properties              Shared
• class-level variables   Shared
BUT
• local variables         Static

Why there is Static and not Shared in case of local variables? Does different keyword indicate a different concept? Or is it only due to historical reasons?

To be clear – I understand that both static and shared variables are allocated on heap instead of stack and retain their value independently of instances. I would expect the same keyword then.

Best Answer

Isn't it correct to say that if something is Shared then you know there will be only one instance of it throughout the whole application. However if something is Static then there could well be many live instances of class but each instance will retain it's own value after termination of the procedure it lives in? Conceptually they are different.

Behavior

When you declare a static variable in a Shared procedure, only one copy of the static variable is available for the whole application. You call a Shared procedure by using the class name, not a variable that points to an instance of the class.

When you declare a static variable in a procedure that isn't Shared, only one copy of the variable is available for each instance of the class. You call a non-shared procedure by using a variable that points to a specific instance of the class.

https://msdn.microsoft.com/en-us/library/z2cty7t8.aspx