• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

C# Equivalent to VB Local Static Variables

The closest equivalent to VB's local Static variable is a private field.  If (and only if) the local static variable was declared in a Shared method, then the C# field should be declared as static.  Instant C# creates new fields for each local Static variable found.  The field name is prefixed by the method name to ensure uniqueness.

VB C#
Public Class StaticLocals
    Public Sub MethodOne()
        Static i As Integer
        ...
        j = i
    End Sub

    Public Shared Sub MethodTwo()
        Static i As Integer
        ...
        j = i
    End Sub
End Class
public class StaticLocals
{
    private int MethodOne_i;
    private static int MethodTwo_i;

    public void MethodOne()
    {
        //...
        j = MethodOne_i;
    }

    public static void MethodTwo()
    {
        //...
        j = MethodTwo_i;
    }
}


Use Instant C# and Instant VB to convert between VB.NET and C#.     

Additional resource: VB.NET and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.