The requested page is not available for the requested platform. You are viewing the content for Default platform.

Boolean to Enum

  • 2 min to read

Converts a boolean type to an enumeration, updating client code if necessary, and activates Linked Identifiers for the enum references.

Purpose

This refactoring is useful if you need to extend your code's logic. Using Boolean to Enum, you can convert two-state structures (method return types or method parameter types) into multiple-state structures.

Availability

Available from the context menus or via shortcuts:

  • when the caret is on a Boolean member, variable or method parameter.
Note

The refactoring supports only .NET languages.

Notes

  • Boolean to Enum declares a new enumeration with two values - Success and Failure. These values substitute true and false respectively.
  • Rename is automatically invoked for the newly declared enumeration and its members.
  • If the converted member is used in expressions, it is replaced by its comparison to the Success enumeration value.
  • A Marker is dropped at the member declaration so that you can easily continue coding after specifying proper names for the enumeration and its members.

Example

class TestClass
{
    private int TestMethod(bool a)
    {
        if (a)
            return 100;
        else
            return 10;
    }
}
Public Class TestClass
    Private Function TestMethod(ByVal a As Boolean)
        If (a) Then
            Return 100
        Else
            Return 10
        End If
    End Function
End Class

Result:

class TestClass
{
    private int TestMethod(TestMethodParam a)
    {
        if (a == TestMethodParam.Success)
            return 100;
        else
            return 10;
    }
}
public enum TestMethodParam
{
    Success,
    Failure
}
Public Class TestClass
    Private Function TestMethod(ByVal a As TestMethodParam)
        If (a = TestMethodParam.Success) Then
            Return 100
        Else
            Return 10
        End If
    End Function
End Class
Public Enum TestMethodParam
    Success
    Failure
End Enum

Animation

rsBooleanToEnumCSharp