Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.

Similar presentations


Presentation on theme: "Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time."— Presentation transcript:

1 Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time

2 Clearly Visual Basic: Programming with Visual Basic 2008 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 2

3 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 3

4 4Clearly Visual Basic: Programming with Visual Basic 2008

5 5

6 6

7 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 7Clearly Visual Basic: Programming with Visual Basic 2008

8 8 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

9 9Clearly Visual Basic: Programming with Visual Basic 2008

10 10Clearly Visual Basic: Programming with Visual Basic 2008

11 Don’t Be So Sensitive (continued) 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 11

12 Fitness for Good Health 12 Dim strCode as String strCode = txtCode.Text If 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”)

13 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 13Clearly Visual Basic: Programming with Visual Basic 2008

14 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 14Clearly Visual Basic: Programming with Visual Basic 2008

15 15Clearly Visual Basic: Programming with Visual Basic 2008

16 16Clearly Visual Basic: Programming with Visual Basic 2008

17 Fitness for Good Health- Case Statement Dim strCode as String = txtCode.Text Select Case StrCode.ToUpper Case “S” intMonthlyFee = 40 Case “F” intMonthlyFee = 50 FINISH THIS CASE STATEMENT Case Else intMontlyFee =0 End Select lblMonthlyFee.Text = intMonthyFee.ToString(“C0”)

18 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 18Clearly Visual Basic: Programming with Visual Basic 2008

19

20 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 20Clearly Visual Basic: Programming with Visual Basic 2008

21 21Clearly Visual Basic: Programming with Visual Basic 2008

22 22Clearly 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

23 23Clearly 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

24 24Clearly 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

25 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 25

26 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 26Clearly Visual Basic: Programming with Visual Basic 2008

27 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


Download ppt "Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time."

Similar presentations


Ads by Google