Page 166 - Open Soource Technologies 304.indd
P. 166

Event Driven Programming


                          Private Sub btnNumber_Click(...) Handles btnNumber.Click
                            ‘Determine the number of days in a season
                            Dim season As String
                            season = txtSeason.Text
                            txtNumDays.Text = season & “ has “ & NumDays(season) & “ days.”
                          End Sub
                          Function NumDays(ByVal season As String) As Integer
                            ‘Look up the number of days in a given season
                          Select case season.ToUpper
                               Case “WINTER”
                                Return 87
                               Case “SPRING”
                                Return 92
                               Case “SUMMER”, “AUTUMN”, “FALL”
                                Return 93
                            End Select
                          End Function
                          [Run, type “Summer” into the text box, and press the button.]

















                          Comments

                            1. In a  Case clause of the form Case b To c, the value of b should be less than or equal
                               to the value of c. Otherwise, the clause is meaningless.
                            2. If the word  Is, which should precede an inequality sign in a value list, is accidentally
                               omitted, the editor will automatically insert it when checking the line.
                            3. The items in the value list must evaluate to a literal of the same type as the selector. For
                               instance, if the selector evaluated to a string value, as in
                            4. Dim firstName As String
                            5. firstName = txtBox.Text

                            6. Select Case firstName
                               then the clause  Case firstName.Length would be meaningless.




                          160                    LOVELY PROFESSIONAL UNIVERSITY
   161   162   163   164   165   166   167   168   169   170   171