The Select Case Statement Select Case Statement Similar

  • Slides: 12
Download presentation
The Select Case Statement

The Select Case Statement

Select Case Statement Similar to If…Then…Else. If § Performs a series of tests §

Select Case Statement Similar to If…Then…Else. If § Performs a series of tests § Conditionally executes the first true condition Select Case is different in that: § A single test expression may be evaluated § The test expression is listed once § The possible values of the expression are then listed with their conditional statements Case Else may be included and executed if none of the values match the expression Slide 4 - 2

Find Day of Week With Select Case CInt(txt. Input. Text) Case 1 Message. Box.

Find Day of Week With Select Case CInt(txt. Input. Text) Case 1 Message. Box. Show("Day 1 is Monday. ") Case 2 Message. Box. Show("Day 2 is Tuesday. ") Case 3 Message. Box. Show("Day 3 is Wednesday. ") Case 4 Message. Box. Show("Day 4 is Thursday. ") Case 5 Message. Box. Show("Day 5 is Friday. ") Case 6 Message. Box. Show("Day 6 is Saturday. ") Case 7 Message. Box. Show("Day 7 is Sunday. ") Case Else Message. Box. Show("That value is invalid. ") End Select Slide 4 - 3

Select Case With Multiple Values Select Case str. Animal Case "Dogs", "Cats" Message. Box.

Select Case With Multiple Values Select Case str. Animal Case "Dogs", "Cats" Message. Box. Show ("House Pets") Case "Cows", "Pigs", "Goats" Message. Box. Show ("Farm Animals") Case "Lions", "Tigers", "Bears" Message. Box. Show ("Oh My!") End Select Slide 4 - 4

Select Case with Operators Select Case int. Score Case Is >= 90 str. Grade

Select Case with Operators Select Case int. Score Case Is >= 90 str. Grade = “A” Case 80 to 89 str. Grade = “B” Case 70 to 79 str. Grade = “C” Case 60 to 69 str. Grade = “D” Case 0 to 59 str. Grade = “F” Case Else Message. Box. Show(“Invalid Score” End Select Slide 4 - 5

Calculate Commission 6

Calculate Commission 6

Introduction to Input Validation

Introduction to Input Validation

Validation Example Output is only as good as the input § “Garbage in, garbage

Validation Example Output is only as good as the input § “Garbage in, garbage out” Input validation is the process of inspecting user input to see that it meets certain rules The Try. Parse method verifies that an input value is in a valid numeric or date format Decision structures are often used to validate input Slide 4 - 8

The Try. Parse Method Converts an input value to another format § Verifies that

The Try. Parse Method Converts an input value to another format § Verifies that input of integers, decimals, dates, etc. , are entered in an acceptable format § Returns Boolean value indicating True if conversion successful § Returns False if unsuccessful Each numeric variable type has a Try. Parse method Date & Boolean types include the Try. Parse method as well Slide 4 - 9

Verify Integer Entry With Try. Parse Use Integer. Try. Parse method to convert value

Verify Integer Entry With Try. Parse Use Integer. Try. Parse method to convert value § § txt. Input. Text contains numeric string to convert int. Result receives converted value Try. Parse returns True if input is an integer Try. Parse returns False if input is not an integer Dim int. Result As Integer If Integer. Try. Parse(txt. Input. Text, int. Result) Then lbl. Message. Text = "Success!" Else lbl. Message. Text = "Error: an integer was not found" End If Slide 4 - 10

Verify Date Entry With Try. Parse Use Date. Try. Parse method to convert value

Verify Date Entry With Try. Parse Use Date. Try. Parse method to convert value § § § txt. Input. Text contains date string to convert dat. Birth receives converted value Try. Parse returns True if input in date format Try. Parse returns False if input not a valid date Not used so Then clause indicates invalid date Dim dat. Birth As Date If Not Date. Try. Parse(txt. Input. Text, dat. Birth) Then lbl. Message. Text = “Not a valid date!“ End If Slide 4 - 11

Using If To Check Range of Values Decision structures often used to validate input

Using If To Check Range of Values Decision structures often used to validate input Example verifies that entries for both dec. Sales and dec. Advance are positive numbers ' Validate the input to ensure that ' no negative numbers were entered. If dec. Sales < 0 Or dec. Advance < 0 Then Message. Box. Show("Please enter positive numbers" & _ " for sales and/or advance pay. “, “Error”) End. If Slide 4 - 12