Expression List Variations Select..Case Statement VB 2002-2005

The Select…Case statement is used to execute one of several groups of statements, depending on the value of an expression. This article and source code reviews and demonstrates different possible ways to vary the expression list used to test the value of an expression in a Select…Case statement.

Screen Shot of Sample Application

Expression List

An expression list is is a list of expression clauses. Multiple expression clauses are separated by commas. The code below shows where expressions lists appear in a Select…Case statement:

            Select Case scores(i)

                Case 100                        ‘ Expression list with one expression.

                    perfectGrade += 1

                    gradeA += 1

                Case Is < 0, Is > 100      ‘ Expresssion list multiple expressions.

                    invalidGrade += 1

            End Select

        Next

 

Each expression clause can take one of the following forms:

  • expression1 To expression2
  • Is ] comparisonoperator expression
  • expression

Use the To keyword to specify the boundaries of a range of match values for testexpression. The value of expression1 must be less than or equal to the value of expression2.

Use the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.

The form specifying only expression is treated as a special case of the Is form where comparisonoperator is the equal sign (=). This form is evaluated as testexpression = expression.

The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.

Source Code Excerpt

    Private Sub CountGradesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountGradesButton.Click

        ‘ Code to determine grades from student scores and

        ‘   report number of students achieving each grade.

 

        ‘ Create an array of student scores for this example.

        Dim scores() As Integer = {70, 91, 51, 62, 1001, 100, 95, 86, 81, 60, 82, -2}

 

        ‘ Declare variables to hold grade counts.

        Dim gradeA, gradeB, gradeC, gradeD, gradeF, perfectGrade, invalidGrade As Integer

 

        ‘ Iterate through student scores.

        Dim i As Integer

        For i = 0 To scores.GetUpperBound(0)

 

            ‘ Use Select..Case statement to determine

            ‘   and count grades.

            Select Case scores(i)

                Case 100        ‘ Explicit value

                    perfectGrade += 1

                    gradeA += 1

                Case 90 To 99   ‘ Range

                    gradeA += 1

                Case 80 To 89

                    gradeB += 1

                Case 70 To 79

                    gradeC += 1

                Case 60 To 69

                    gradeD += 1

                Case 0 To 59

                    gradeF += 1

                Case Is < 0, Is > 100 ‘ Is keyword with comparrison operator (multiple).

                    invalidGrade += 1

            End Select

        Next

 

        ‘ Report Grades.

        Me.GradeCountsRichTextBox.Clear()

        Me.GradeCountsRichTextBox.AppendText( _

                “Results” & vbCrLf & vbCrLf & _

                “Perfect Grades: “ & perfectGrade & vbCrLf & _

                “Invalid Grades: “ & invalidGrade & vbCrLf & _

                “A: “ & gradeA & vbCrLf & “B: “ & gradeB _

                & vbCrLf & “C: “ & gradeC & vbCrLf & “D: “ & _

                gradeD & vbCrLf & “F: “ & gradeF)

    End Sub

For more information visit the link below:

Select…Case Statement (Visual Basic)

~ bởi duriangroup on Tháng Sáu 1, 2007.

 
%d bloggers like this: