270 likes | 292 Views
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/ElseIf/Else Declare a variable using the String data type Convert a string to uppercase or lowercase
E N D
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/ElseIf/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
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
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 • ToUpper method • Used to convert a string to uppercase • ToLower method • Used to convert a string to lowercase Clearly Visual Basic: Programming with Visual Basic 2008
Don’t Be So Sensitive (continued) When using the ToUpper method in a comparison: Everything you are comparing should be in uppercase When using the ToLower method in a comparison: Everything you are comparing should be in lowercase Clearly Visual Basic: Programming with Visual Basic 2008
When processing the statement: strCode = txtCode.Text.ToUpper The computer first makes a temporary copy of the string entered in the txtCode control It then converts the copy to uppercase, storing the result in the strCode variable Finally, it removes the copy from its internal memory Don’t Be So Sensitive (continued) Clearly Visual Basic: Programming with Visual Basic 2008
Fitness for Good Health Dim strCode as StringstrCode = txtCode.TextIf strCode.ToUpper = “S” Then intMonthlyFee = 40 Elseif strCode.ToUpper = “F” Then intMonthlyFee = 50 Elseif FINISH THIS IF THEN STATEMENT Else intMontlyFee = 0 End IF lblMonthlyFee.Text = intMonthyFee.ToString(“C0”)
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 selectorExpression • selectorExpression • Can contain any combination of variables, constants, methods, operators, or properties Clearly Visual Basic: Programming with Visual Basic 2008
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 expressionList Clearly Visual Basic: Programming with Visual Basic 2008
Fitness for Good Health- Case Statement Dim strCode as String = txtCode.Text Select Case StrCode.ToUpper Case “S” intMonthlyFee = 40 Case “F” intMonthlyFee = 50FINISH THIS CASE STATEMENT Case Else intMontlyFee =0 End Select lblMonthlyFee.Text = intMonthyFee.ToString(“C0”)
Specifying a Range of Values in a Case Clause’s Expression List • You can specify a range of values in a Case clause’s expressionList • 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
Using Radio Buttons • If/ElseIf/Else and Case forms of the selection structure • Often used when coding interfaces that contain radio buttons • Radio button • Created using the RadioButton 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
HERE IS ANOTHER IDEA:Dim intShipping As Integer If radAlabama.Checked = True Then intShipping = 20 ElseIf radGeorgia.Checked = True Then intShipping = 35 ElseIf radLouisiana.Checked = True Then intShipping = 30 ElseIf radNorthCarolina.Checked = True Then intShipping = 28 End If If radOvernight.Checked = True Then intShipping = intShipping + 10 ElseIf radTwoDay.Checked = True Then intShipping = intShipping + 5 End If Clearly Visual Basic: Programming with Visual Basic 2008
HERE IS ANOTHER IDEA:Dim intShipping As Integer Select Case True Case radAlabama.Checked intShipping = 20 Case radGeorgia.Checked intShipping = 35 Case radLouisiana.Checked intShipping = 30 Case radNorthCarolina.Checked intShipping = 28 End Select If radOvernight.Checked = True Then intShipping = intShipping + 10 ElseIf radTwoDay.Checked = True Then intShipping = intShipping + 5 End If Clearly Visual Basic: Programming with Visual Basic 2008
HERE IS ANOTHER IDEA with a Sub : If radAlabama.Checked = True Then If radStandard.Checked = True Then intShipping = 25 ElseIf radOvernight.Checked = True Then intShipping = 35 ElseIf radTwoDay.Checked = True Then intShipping = 30 End If End If Clearly Visual Basic: Programming with Visual Basic 2008
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
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
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