In VB6 there are local static variables that keep their values after the exit of procedure. It's like using public vars but on local block. For example:
sub count()
static x as integer
x = x + 1
end sub
After 10 calls, x will be 10. I tried to search the same thing in .NET (and even Java) but there was none. Why? Does it break the OOP model in some way, and is there a way to emulate that.
The closest you can get is a static field outside the method:
private static int x;
public [static] void Foo() {
x++;
}
Closure example as requested:
using System;
class Program {
private static readonly Action incrementCount;
private static readonly Func<int> getCount;
static Program() {
int x = 0;
incrementCount = () => x++;
getCount = () => x;
}
public void Foo() {
incrementCount();
incrementCount();
Console.WriteLine(getCount());
}
static void Main() {
// show it working from an instance
new Program().Foo();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With