Defining Default Behavior for Select Case and If-Then-Else Statements

Unpredictable results can occur when no default action is defined for Select Case statements or If-Then-Else statements.

Recommendations

To avoid unpredictable results, always define default behavior when using Select Case of If-Then-Else statements.

The following are examples of incorrect and correct ways to define default behavior for situations not covered by the existing Select Case and If-Then-Else statements.

Incorrect Usage

The author of this subroutine intends for the BG_USER_01 field to be visible only if the status of the defect is Open, New, or Reopen. However, if the IsVisible property of a Closed or Fixed defect was set to True prior to the instance of this subroutine, that Closed or Fixed defect will also be visible. This is because there is no case statement defined specifically for Closed and Fixed defects.

Example:

Sub Bug_FieldChange(FieldName)
    If FieldName="BG_STATUS" Then
        Select Case Bug_Fields(FieldName).Value
            Case "Open", "New", "Reopen" _
	     Bug_Fields("BG_USER_01").IsVisible = True
        End Select
    End If
End Sub

 

Correct Usage

This subroutine effectively handles all possible cases.

Example:

Sub Bug_FieldChange(FieldName)
    If FieldName="BG_STATUS" Then
        Select Case Bug_Fields(FieldName).Value
            Case "Open", "New", "Reopen"
                Bug_Fields("BG_USER_01").IsVisible = True
            Case Else
                Bug_Fields("BG_USER_01").IsVisible = False
        End Select
    End If
End Sub