When is a static variable created in Visual Basic?

Say I have a static variable in a function:

Private Sub SomeFunction()
    Static staticVar As String = _myField.Value
End Sub

When, exactly, is this value from the _myField assigned to staticVar? When you first call the function? Instant surrounding class?

+5
source share
3 answers

A Staticvariable is created when it is assigned, obviously.

If this line is started, not earlier, but not after.

Put a breakpoint on it and you will see.

Static means that the value will be saved between calls.

Shared / Shared , / Shared. , . Static , Static #, .


, , , Shared, Shared .

, , , , . .

+2

CLR , VB.NET .

. , , . , . , . Shared, Shared . .

, . , , Dim . . , . , , , .

StaticLocalInitFlag , , . - Monitor.Enter() . , SyncLock. StaticLocalInitFlag , , , .

, Try finally . Try StaticLocalInitFlag.State 0. . 2, , . . , , 2. , - , IncompleteInitialization.

finally 1, , . Monitor.Leave().

, 96 . Static , .

+8

The VB.NET compiler creates a static (shared in VB.NET) class variable to maintain the value of "staticVar". Thus, it is initialized, like any other static / shared variable, the first time you use the static field of this class (or when you call this method).

http://weblogs.asp.net/psteele/pages/7717.aspx

+1
source

All Articles