The Select Case...End Select Structure
The format of the Select Case control structure is show below:
| Example 10.1 ' Examination Grades Dim grade As String Private Sub Compute_Click( ) grade=txtgrade.Text Select Case grade Case "A" Label1.Text="High Distinction" Case "A-" Label2.Text="Distinction" Case "B" Label3.Text="Credit" Case "C" Label4.Text="Pass" Case Else Label5.Text="Fail" End Select |
Example 10.2 In this example, you can use the keyword Is together with the comparison operators. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Examination Marks Dim mark As Single mark = mrk.Text Select Case mark Case Is >= 85 Label1.Text= "Excellence" Case Is >= 70 Label2.Text= "Good" Case Is >= 60 Label3.Text = "Above Average" Case Is >= 50 Label4.Text= "Average" Case Else Label5.Text = "Need to work harder" End Select End Sub | Example 10.3 Example 10.2 could be rewritten as follows: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Examination Marks Dim mark As Single mark = mrk.Text Select Case mark Case 0 to 49 Label1.Text = "Need to work harder" Case 50 to 59 Label2.Text = "Average" Case 60 to 69 Label3.Text= "Above Average" Case 70 to 84 Label4.Text = "Good" Case Else Label5.Text= "Excellence" End Select End Sub |
Tidak ada komentar:
Posting Komentar