Upload
stanley-wilkinson
View
219
Download
0
Embed Size (px)
DESCRIPTION
Chapter 43 String Comparison in If Statement John (same) JOHN (different)
Citation preview
Decisions with Select Case and Strings
Chapter 4 Part 2
Chapter 4 2
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
Chapter 4 3
String Comparison in If Statement
John (same)JOHN (different)
Chapter 4 4
More String Comparison
John (equal) JOHN (less than John) Johnny (greater than John) Jones (greater than John)
Chapter 4 5
How Strings Are Compared
J O H N
J O N E S
Chapter 4 6
ToUpper
StringExpression.ToUpper Returns uppercase equivalent of string. Does not change original string contents.
Then store result in another variable.
Chapter 4 7
ToLower
StringExpression.ToLower Returns lowercase equivalent of string. Does not change original string contents.
Then store result in another variable.
Chapter 4 8
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
Chapter 4 9
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)
Chapter 4 10
Spaces
Leading Spaces Spaces (shown here with #) before actual
characters #####Keith
Trailing Spaces Spaces after actual characters Keith#####
Chapter 4 11
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.
Chapter 4 12
TrimStart Example
See p. 227 for displayingmultiple lines.
Chapter 4 13
Other String Expressions
Substring (pp. 218-219) IndexOf (pp. 219-220)
Chapter 4 14
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 10th position. Eventful adventure (string) advent (substring
Chapter 4 15
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.
Chapter 4 16
Trim Full Name Example
Chapter 4 17
Trim Full Name, Continued
Chapter 4 18
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
Chapter 4 19
Select Case Exact Match Example
Chapter 4 20
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 or4) Executes the Case Else statement block if
no match is selected.
Chapter 4 21
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
Chapter 4 22
Select Case Example
txtTest btnGrade
90+ Display A and “Superior” 80-89Display B and “Good” 70-79Display C and “Satisfactory” OtherDisplay failing notice
Chapter 4 23
Relational Operator >=And Range Selection
Chapter 4 24
Summary
Select Case If 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.
Chapter 4 25
Decision Rules
Select Case If 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.
Chapter 4 26
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 youselect another radio button
Chapter 4 27
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.
Chapter 4 28
Radio Button Properties
Name Control name, such as radRed
Text Descriptive text displayed by the circle
Checked Selection option value = TrueNot selected value = False
TabIndex Helps with focus (active control)
Chapter 4 29
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.
Chapter 4 30
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 ThenMessageBox.Show(“You chose red.”)
ElseIf radGreen.Checked = True ThenMessageBox.Show(“You chose green.”)
ElseIf radBlue.Checked = True ThenMessageBox.Show(“You chose blue.”)
ElseIf radPurple.Checked = True ThenMessageBox.Show(“You chose purple.”)
End If
Chapter 4 31
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.
Chapter 4 32
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).
Chapter 4 33
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
Chapter 4 34
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.
Chapter 4 35
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