Declare Enum

Declares an enumeration type for the current reference. Places the cursor on the generated enum contents and drops a marker onto the initial enum reference.

The Declare Enum code provider also declares enum elements called from the initial code.

Availability

From the context menus or via shortcuts:

  • when the edit cursor or caret is on a reference to a non-existent enumeration type.

Example

private void TestMethod(MyEnum enumVar)
{
    int i;
    switch (enumVar)
    {
        case MyEnum.A:
            i = 1;
            break;
        case MyEnum.B:
            i = 2;
            break;
        case MyEnum.C:
            i = 3;
            break;
    }
}
Public Class TestClass
    Private Sub TestMethod(enumVar As MyEnum)
        Dim i As Integer
        Select Case enumVar
            Case MyEnum.A
                i = 1
            Case MyEnum.B
                i = 2
            Case MyEnum.C
                i = 3
        End Select
    End Sub
End Class

Result:

public enum MyEnum
{
    A,
    B,
    C
}
class TestClass
{
    private void TestMethod(MyEnum enumVar)
    {
        int i;
        switch (enumVar)
        {
            case MyEnum.A:
                i = 1;
                break;
            case MyEnum.B:
                i = 2;
                break;
            case MyEnum.C:
                i = 3;
                break;
        }
    }
}
Public Enum MyEnum
    A
    B
    C
End Enum
Public Class TestClass
    Private Sub TestMethod(enumVar As MyEnum)
        Dim i As Integer
        Select Case enumVar
            Case MyEnum.A
                i = 1
            Case MyEnum.B
                i = 2
            Case MyEnum.C
                i = 3
        End Select
    End Sub
End Class