Let’s say I have a static variable in a function:
Private Sub SomeFunction()
Static staticVar As String = _myField.Value
End Sub
When, exactly, is the value from _myField assigned to staticVar? Upon the first call to the function? Instantiation of the enclosing class?
A
Staticvariable is instantiated when it is assigned, obviously.When that line is run, not before, not after.
Put a breakpoint on it and you’ll see.
Staticjust means that the value will persist between calls.Sharedclass/module members are instantiated the first time the class is accessed, before anySharedconstructor and before the call, whether that is to the class constructor or someSharedmethod/property. I think this may be the origin of your confusion.Staticlocal variables, like localstatics in c# are not instantiated in this way.Interesting information from Tim Schmelter, it appears the value is maintained internally using a hidden
Sharedclass level variable that is instantiated like otherSharedclass level variables but always with the default value.The effect observed by you the developer is unchanged, apart from some instantiation delay when the class is accessed. This delay should be undetectable in practice.