Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's a way of having fields that are local to a method. Basically, the value is maintained between calls but not accessible in other parts of the class. See <a href="http://weblogs.asp.net/psteele/pages/7717.aspx" rel="nofollow noreferrer">Static Local Variables in VB.NET</a> for some implementation information.</p> <p>EDIT: Jonathan, you're right that the fields don't have to be Shared/static. If the function/sub is declared Shared, it will be a Shared/static field. Otherwise, it will be a instance field. Either way, it is persistent across calls and local to the method. The below example (to continue <a href="https://stackoverflow.com/questions/2282615/ruby-equivalent-of-cs-yield-keyword-or-creating-sequences-without-prealloca/2282658#2282658">a theme</a>) shows both behaviors clearly:</p> <pre><code>Class Fibonacci Public Function FibonacciInstance() as Integer Static i as Integer = -1 Static j as Integer = 1 Dim k as Integer k = i + j i = j j = k return k End Function Public Shared Function FibonacciShared() as Integer Static i as Integer = -1 Static j as Integer = 1 Dim k as Integer k = i + j i = j j = k return k End Function Shared Sub Main() Dim d as Integer Dim a = New Fibonacci Dim b = New Fibonacci For d = 0 to 10 System.Console.WriteLine("a.FibonacciInstance: " &amp; a.FibonacciInstance()) System.Console.WriteLine("b.FibonacciInstance: " &amp; b.FibonacciInstance()) System.Console.WriteLine("a.FibonacciShared: " &amp; a.FibonacciShared()) System.Console.WriteLine("b.FibonacciShared: " &amp; b.FibonacciShared()) Next d End Sub End Class </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload