Skip to main content

Conditional to Case / Case to Conditional

  • 2 minutes to read

Converts nested if-else blocks into a switch (Select) statement or vice versa.

#Purpose

While refactoring your code, you sometimes need to convert a switch (Select) statement into nested if-else blocks or vice versa. This makes it easier for you to carry out the required changes. While such conversions usually require a lot of formatting work, Refactor! allows you to accomplish this with just a single click.

#Availability

Available from the context menu or via shortcuts:

  • when the caret or edit cursor is on, an if statement that has a corresponding else block.
  • when the caret or edit cursor is on, a switch (Select) statement.

#Example

int i = 10;
switch (i)
{
    case 1:
        i++;
        break;
    case 2:
        i--;
        break;
    default:
        i = 10;
        break;
}
Select Case i
Case 1
    i = i + 1
Case 2
    i = i - 1
Case Else
    i = 10
End Select

Result:

int i = 10;
if (i == 1)
    i++;
else
{
    if (i == 2)
        i--;
    else
        i = 10;
}
If i = 1 Then
    i = i + 1
Else
    If i = 2 Then
       i = i - 1
    Else
       i = 10
    End If
End If

#Animation

rsConditionalToCaseToConditional