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

Case statement has incorrect range of integers expression (VB)

CodeRush Classic shows the Case statement has incorrect range of integers expression (VB) code issue if the Case statement has an incorrect range of integers. For instance, 10 - 20 instead of 10 To 20.

Note

This code issue is available only in Visual Basic code.

Fix

Correct the range of integers in the Case statement.

Purpose

Highlights Case statements with values specified as "A - B". For example, Case 10 - 20. This statement is equivalent to Case -10, because the compiler recognizes "10 - 20" as a subtraction operation. Use the To keyword instead of "-" to correctly specify the range of integers in a Case statement.

Example

Public Function GetWeightCategory(weight As Integer) As String
    Select Case weight
        Case 0 - 10
            Return "light"
        Case 11 - 30
            Return "medium"
        Case 31 - 50
            Return "heavy"
        Case Else
            Return "too heavy to lift"
    End Select
End Function

Fix:

Public Function GetWeightCategory(weight As Integer) As String
    Select Case weight
        Case 0 To 10
            Return "light"
        Case 11 To 30
            Return "medium"
        Case 31 To 50
            Return "heavy"
        Case Else
            Return "too heavy to lift"
    End Select
End Function