Download presentation
Presentation is loading. Please wait.
Published byStanley Wilkinson Modified over 9 years ago
1
Decisions with Select Case and Strings Chapter 4 Part 2
2
Chapter 42 String Storage (Section 4.8) Stores characters (e.g., A, a, B, b) as numeric codes in Unicode format. Displays character corresponding to actual Unicode number stored in memory. Arranges letters in alphabetical order in Unicode sequencing. A comes before B. A has lower Unicode number than B. Uppercase before lowercase: A before a Space before letters
3
Chapter 43 String Comparison in If Statement John (same) JOHN (different)
4
Chapter 44 More String Comparison John (equal) JOHN (less than John) Johnny (greater than John) Jones (greater than John)
5
Chapter 45 How Strings Are Compared JOHN JONES
6
Chapter 46 ToUpper StringExpression.ToUpper Returns uppercase equivalent of string. Does not change original string contents. Then store result in another variable.
7
Chapter 47 ToLower StringExpression.ToLower Returns lowercase equivalent of string. Does not change original string contents. Then store result in another variable.
8
Chapter 48 Tutorial 4-6 (pp. 214-216) Illustrates how to use ToUpper within an If statement to process comparisons. Shows that typing “prospero” equals “PROSPERO” when using: txtInput.Text.ToUpper
9
Chapter 49 StringExpression.Length Determines the length of a string. Text Box Example: Visual Basic (typed in text box) txtProgram.Text.Length Length = 17 Variable Example: strName = txtName.Text intNumber = strName.Length Good for getting restricted # of characters from user. (see p. 217)
10
Chapter 410 Spaces Leading Spaces Spaces (shown here with #) before actual characters #####Keith Trailing Spaces Spaces after actual characters Keith#####
11
Chapter 411 Trim Methods Use Trim method to remove spaces. StringExpression.TrimStart Removes leading spaces #####Keith becomes Keith StringExpression.TrimEnd Removes trailing spaces Keith##### becomes Keith StringExpression.Trim Removes leading and trailing spaces #####Keith##### becomes Keith Trim methods do not modify actual variable; use Trim and store results in another variable.
12
Chapter 412 TrimStart Example See p. 227 for displaying multiple lines.
13
Chapter 413 Other String Expressions Substring (pp. 218-219) IndexOf (pp. 219-220)
14
Chapter 414 InStr Searches for a substring within the base string (searches are case-sensitive). Proceeds left to right. Stops at match or end of string. If successful, returns the character position at which the match was found. If unsuccessful, returns 0. Example: InStr("Eventful adventure","advent") Returns 10 because “a” starts in 10 th position. Eventful adventure (string) advent (substring
15
Chapter 415 More InStr() Instr(startpos, basestring, searchstring) Example: InStr(1,”Eventful adventure”, “vent”) Starts at position 1. Looks in Eventful adventure. Finds starting position of v in vent. Returns 2. Example: InStr(1, strWholeName, “,“) Looks within variable contents to find position of comma.
16
Chapter 416 Trim Full Name Example
17
Chapter 417 Trim Full Name, Continued
18
Chapter 418 Select Case Handles conditions with multiple outcomes. Tests one expression, whereas ElseIf tests several expressions. Select Case testexpression Case expressionlist1 Statementblock1 Case expressionlist2 Statementblock2 Case Else Statementblock End Select
19
Chapter 419 Select Case Exact Match Example
20
Chapter 420 Select Case at Run Time 1) Evaluates the test expression. 2) Attempts to match the resulting value with one of the expression lists. 1) Starts searching top of expression list. 2) Proceeds through subsequent expression lists, stopping at the first match. 3) Processes code at match or 4) Executes the Case Else statement block if no match is selected.
21
Chapter 421 Select Case for Ranges Select Case expression Case Is < 1 ‘requires Is before relational operator Do something Case 1 To 5 ‘requires To keyword between values Do something different Case 6 To 10 Do something Case Else Do something entirely different. End Select
22
Chapter 422 Select Case Example txtTest btnGrade 90+ Display A and “Superior” 80-89Display B and “Good” 70-79Display C and “Satisfactory” OtherDisplay failing notice
23
Chapter 423 Relational Operator >= And Range Selection
24
Chapter 424 Summary Select CaseIf Statement Test expression can be any numeric or string expression. Must be a logical expression, such as > <. Decision determined by a single expression at the top. Has one condition at the top and one for each ElseIf and each of these conditions is a separate expression. Appropriate for more “clear cut” examples. Enables more complexity than Select Case.
25
Chapter 425 Decision Rules Select CaseIf Statement Decision has multiple outcomes, which depend only on a single expression. Decision has only 2 outcomes & decision can be expressed as a single expression. Outcome depends on multiple conditions that may be independent of each other.
26
Chapter 426 Radio Button Control (p. 236) Ensures that the user selects only one option: Male/Female Age ranges Circle with descriptive text Only 1 selected in group at a time Mutually Exclusive: Deselects one when you select another radio button
27
Chapter 427 Radio Button Rules & Conventions Use radControlName style with rad prefix. Use access keys on Text property. Set TabIndex from one radio button to another. Must use group boxes if the form has more than one set of radio buttons (see bulleted list and examples on p. 236). Use no more than 7 radio buttons per set.
28
Chapter 428 Radio Button Properties NameControl name, such as radRed TextDescriptive text displayed by the circle CheckedSelection option value = True Not selected value = False TabIndexHelps with focus (active control)
29
Chapter 429 Radio Button Results Can trigger Click event, but typically enable user to choose and then click a button to trigger event. Default radio button Set Checked property to True.
30
Chapter 430 Which button is clicked? Use in IF…ElseIf statement to determine if radio button Checked property value is true (i.e., selected). If radRed.Checked = True Then MessageBox.Show(“You chose red.”) ElseIf radGreen.Checked = True Then MessageBox.Show(“You chose green.”) ElseIf radBlue.Checked = True Then MessageBox.Show(“You chose blue.”) ElseIf radPurple.Checked = True Then MessageBox.Show(“You chose purple.”) End If
31
Chapter 431 Check Box Control (p. 238) Not Mutually Exclusive: Enables user to select 0, 1, or more options in same category. Name: chk prefix standard chkBold chkItalic chkUnderline Text: caption displayed onscreen Checked: selected if True Can set 1 or more at design time See characteristics on p. 238.
32
Chapter 432 Is a check box checked? Although If…ElseIf statements work well for radio button groups, they do not work well for check boxes. Use individual If statements for each check box to see if it is checked. Use Checked to determine if check box is selected. chkBold.Checked = True Compare radio button code (p. 239) to check box code (p. 240).
33
Chapter 433 Class-Level Variables Review scope of a variable. Visible and accessible to statements Local variables Declared within event procedure; local to that procedure Class-level variables Declared at the form level; available to all procedures on that form
34
Chapter 434 Class-Level Concerns Wrong value can be stored; must track down code that causes problem (very troublesome in complex programs). When 2 or more procedures modify same variable, must be careful that 1 procedure doesn’t modify it when you need original value in another procedure.
35
Chapter 435 Recommended Practice Tutorial 4-7 (Strings) Tutorial 4-8 (Select Case) Tutorial 4-9 (Check Boxes & Radio Btns) Section 4-14 and Tutorial 10 pp. 241-250 Comprehensive review
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.