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.
-
1This is the kind of thing only the Microsoft devs could answer IMO.RubberDuck– RubberDuck2015年03月04日 15:30:46 +00:00Commented Mar 4, 2015 at 15:30
-
1@RubberDuck - Maybe we found one?JeffO– JeffO2015年03月04日 16:58:41 +00:00Commented Mar 4, 2015 at 16:58
1 Answer 1
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.
-
1Now I see what difference I was missing. Thanks for clarification. Keywords
Shared
andStatic
truly denote different concepts.miroxlav– miroxlav2015年03月04日 15:47:11 +00:00Commented Mar 4, 2015 at 15:47