Clearly Visual Basic Programming with Visual Basic 2008
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time
Objectives • Code a multiple-path selection structure using If/Else • Declare a variable using the String data type • Convert a string to uppercase or lowercase • Code a multiple-path selection structure using Select Case • Include a radio button in an interface Clearly Visual Basic: Programming with Visual Basic 2008 2
Multiple-Path Selection Strategies • Multiple-path selection structures – Can choose from several alternatives • Figure 11 -1 – Contains a problem specification that requires a multiple-path selection structure • Figure 11 -2 – Shows two ways of coding the multiple-path selection structure from Figure 11 -1 Clearly Visual Basic: Programming with Visual Basic 2008 3
Clearly Visual Basic: Programming with Visual Basic 2008 4
5
6
Don’t Be So Sensitive • Case sensitive – String comparisons in Visual Basic • Unicode – Universal coding scheme for characters – Assigns a unique numeric value to each character • To. Upper method – Used to convert a string to uppercase • To. Lower method – Used to convert a string to lowercase Clearly Visual Basic: Programming with Visual Basic 2008 7
Don’t Be So Sensitive (continued) • When using the To. Upper method in a comparison: – Everything you are comparing should be in uppercase • When using the To. Lower method in a comparison: – Everything you are comparing should be in lowercase Clearly Visual Basic: Programming with Visual Basic 2008 8
Clearly Visual Basic: Programming with Visual Basic 2008 9
Clearly Visual Basic: Programming with Visual Basic 2008 10
Don’t Be So Sensitive (continued) • When processing the statement: str. Code = txt. Code. Text. To. Upper – The computer first makes a temporary copy of the string entered in the txt. Code control – It then converts the copy to uppercase, storing the result in the str. Code variable – Finally, it removes the copy from its internal memory Clearly Visual Basic: Programming with Visual Basic 2008 11
Fitness for Good Health Dim str. Code as String str. Code = txt. Code. Text If str. Code. To. Upper = “S” Then int. Monthly. Fee = 40 Elseif str. Code. To. Upper = “F” Then int. Monthly. Fee = 50 Elseif FINISH THIS IF THEN STATEMENT Else int. Montly. Fee = 0 End IF lbl. Monthly. Fee. Text = int. Monthy. Fee. To. String(“C 0”) 12
What’s the Next Case on the Docket? • Figure 11 -7 shows: – The Select Case statement’s syntax – How to use the statement to code • A multiple-path selection structure that displays a message corresponding to a letter grade • Select Case statement – Begins with the keywords Select Case, followed by a selector. Expression • selector. Expression – Can contain any combination of variables, constants, methods, operators, or properties Clearly Visual Basic: Programming with Visual Basic 2008 13
What’s the Next Case on the Docket? (continued) • Select Case Statement – You can have as many Case clauses as necessary – If the Select Case statement includes a Case Else clause: • The Case Else clause must be the last clause in the statement – Each of the individual Case clauses, except the Case Else clause, must contain an expression. List Clearly Visual Basic: Programming with Visual Basic 2008 14
Clearly Visual Basic: Programming with Visual Basic 2008 15
Clearly Visual Basic: Programming with Visual Basic 2008 16
Fitness for Good Health- Case Statement Dim str. Code as String = txt. Code. Text Select Case Str. Code. To. Upper Case “S” int. Monthly. Fee = 40 Case “F” int. Monthly. Fee = 50 FINISH THIS CASE STATEMENT Case Else int. Montly. Fee =0 End Select lbl. Monthly. Fee. Text = int. Monthy. Fee. To. String(“C 0”)
Specifying a Range of Values in a Case Clause’s Expression List • You can specify a range of values in a Case clause’s expression. List – You do this using either the keyword To or the keyword Is – To keyword • Used when you know both the upper and lower bounds of the range – Is keyword • Used when you know only one end of the range Clearly Visual Basic: Programming with Visual Basic 2008 18
Clearly Visual Basic: Programming with Visual Basic 2008
Using Radio Buttons • If/Else and Case forms of the selection structure – Often used when coding interfaces that contain radio buttons • Radio button – Created using the Radio. Button tool in the toolbox – Allows you to limit the user to only one choice in a group of two or more • Default radio button – Radio button that represents the user’s most likely choice or the first radio button in the group Clearly Visual Basic: Programming with Visual Basic 2008 20
Clearly Visual Basic: Programming with Visual Basic 2008 21
HERE IS ANOTHER IDEA: Dim int. Shipping As Integer If rad. Alabama. Checked = True Then int. Shipping = 20 Else. If rad. Georgia. Checked = True Then int. Shipping = 35 Else. If rad. Louisiana. Checked = True Then int. Shipping = 30 Else. If rad. North. Carolina. Checked = True Then int. Shipping = 28 End If If rad. Overnight. Checked = True Then int. Shipping = int. Shipping + 10 Else. If rad. Two. Day. Checked = True Then int. Shipping = int. Shipping + 5 End If Clearly Visual Basic: Programming with Visual Basic 2008 22
HERE IS ANOTHER IDEA: Dim int. Shipping As Integer Select Case True Case rad. Alabama. Checked int. Shipping = 20 Case rad. Georgia. Checked int. Shipping = 35 Case rad. Louisiana. Checked int. Shipping = 30 Case rad. North. Carolina. Checked int. Shipping = 28 End Select If rad. Overnight. Checked = True Then int. Shipping = int. Shipping + 10 Else. If rad. Two. Day. Checked = True Then int. Shipping = int. Shipping + 5 End If Clearly Visual Basic: Programming with Visual Basic 2008 23
HERE IS ANOTHER IDEA with a Sub : If rad. Alabama. Checked = True Then If rad. Standard. Checked = True Then int. Shipping = 25 Else. If rad. Overnight. Checked = True Then int. Shipping = 35 Else. If rad. Two. Day. Checked = True Then int. Shipping = 30 End If Clearly Visual Basic: Programming with Visual Basic 2008 24
Summary • Solutions to some problems require a multiple-path selection structure • You can code a multiple-path selection structure – Using either the If. . . Then. . . Else statement or the Select Case statement • String comparisons are case sensitive • Each character on the computer keyboard – Associated with a unique Unicode value Clearly Visual Basic: Programming with Visual Basic 2008 25
Summary (continued) • Before using a string in a comparison: – You can convert it to either uppercase or lowercase • You can use the To or Is keywords to: – Specify a range of values in a Select Case statement • You can use a radio button to: – Limit the user to one choice from a group of two or more Clearly Visual Basic: Programming with Visual Basic 2008 26
Summary (continued) • To include two groups of radio buttons in an interface: – At least one must be placed within a container • Customary to have: – One radio button in each group of radio buttons selected when the user interface first appears • The Boolean value stored in a radio button’s Checked property – Determines whether the radio button is selected (True) or unselected (False) Clearly Visual Basic: Programming with Visual Basic 2008 27
- Slides: 27