The requirement for a final Case Else
clause is defensive programming. The clause should either take appropriate action or contain a
suitable comment as to why no action is taken. Even when the Select
covers all current values of an enum
, a Case
Else
case should still be used because there is no guarantee that the enum
won’t be extended.
Noncompliant code example
Dim number As Integer = 8
Select Case number 'Non-Compliant, what to do when number is not between 1 and 10 ?
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
End Select
Compliant solution
Dim number As Integer = 8
Select Case number
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
Case Else
Debug.WriteLine("Greater than 10")
End Select