Download presentation
Presentation is loading. Please wait.
1
Chapter 4 Decisions and Conditions
Programming In Visual Basic .NET
2
If Statements Used to make decisions
If true, only the Then clause is executed, if false, only Else clause, if present, is executed Block If…Then…Else must always conclude with End If Then must be on same line as If or ElseIf End If and Else must appear alone on a line Note: ElseIf is 1 word, End If is 2 words © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
3
If…Then…Else – General Form
If (condition) Then statement(s) [ElseIf (condition) Then statement(s)] [Else End If © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
4
If…Then…Else - Example
unitsDecimal = Decimal.Parse(unitsTextBox.Text) If unitsDecimal < 32D Then freshmanRadioButton.Checked = True Else freshmanRadioButton.Checked = False End If © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
5
Conditions Test in an If statement is based on a condition
Six relational operators are used for comparison Negative numbers are less than positive numbers An equal sign is used to test for equality Strings can be compared, enclose strings in quotes (see Page 142 for ANSI Chart, case matters) JOAN is less than JOHN HOPE is less than HOPELESS Numbers are always less than letters 300ZX is less than Porsche © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
6
The Six Relational Operators
Greater Than > Less Than < Equal To = Not Equal To <> Greater Than or Equal To >= Less Than or Equal to <= © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
7
ToUpper and ToLower Methods
Use ToUpper and ToLower methods of the String class to return the uppercase or lowercase equivalent of a string, respectively If nameTextBox.Text.ToUpper( ) = "Basic" Then ' Do something. End If © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
8
Compound Conditions Join conditions using logical operators
Or If one or both conditions True, entire condition is True And Both conditions must be True for entire condition to be True Not Reverses the condition, a True condition will evaluate False and vice versa © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
9
Compound Condition Examples
If maleRadioButton.Checked And _ Integer.Parse(ageTextBox.Text) < 21 Then minorMaleCountInteger += 1 End If If juniorRadioButton.Checked Or seniorRadioButton.Checked Then upperClassmanInteger += 1 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
10
Combining And and Or Example
If saleDecimal > Or discountRadioButton.Checked _ And stateTextBox.Text.ToUpper( ) <> "CA" Then ' Code here to calculate the discount. End If © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
11
Nested If Statements If tempInteger > 32 Then
commentLabel.Text = "Hot" Else commentLabel.Text = "Moderate" End If commentLabel.Text = "Freezing" © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
12
Using If Statements with Radio Buttons & Check Boxes
Instead of coding the CheckedChanged events, use If statements to see which are selected Place the If statement in the Click event for a Button, such as an OK or Apply button Private Sub testButton_Click(ByVal sender As System.Object, _ By Val e As System.EventArgs) Handles testButton.Click ' Test the value of the check box. If testCheckBox.Checked Then messageLabel.Text = "Check box is checked" End If End Sub © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
13
Enhancing Message Boxes
For longer, more complex messages, store the message text in a String variable and use that variable as an argument of the Show method VB will wrap longer messages to a second line Include ControlChars to control the line length and position of the line break © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
14
ControlChars Constants (p 152)
Constant Description CR Carriage Return CRLF Carriage Return + Line Feed NewLine Carriage Return + Line Feed Tab Tab Character NullChar Character with a Value of Zero Quote Quotation Mark Character © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
15
Message Box - Multiple Lines of Output
ControlChars.NewLine Used to force to next line © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
16
Message String Example
Dim formattedTotalString As String Dim formattedAvgString As String Dim messageString As String formattedTotalString = totalSalesDecimal.ToString("N") formattedAvgString = averageSalesDecimal.ToString("N") messageString = "Total Sales: " & formattedTotalString _ & ControlChars.NewLine & "Average Sale: " & _ formattedAvgString MessageBox.Show(messageString, "Sales Summary", _ MessageBoxButtons.OK) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
17
Displaying Multiple Buttons
Use MessageBoxButtons constants to display more than one button in the Message Box Message Box's Show method returns a DialogResult object that can be checked to see which button the user clicked Declare a variable to hold an instance of the DialogResult type to capture the outcome of the Show method © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
18
Message Box - Multiple Buttons
MessageBoxButtons.YesNo © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
19
Declaring a Variable for the Method Return
Dim whichButtonDialogResult As DialogResult whichButtonDialogResult = MessageBox.Show _ ("Clear the current order figures?", "Clear Order", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) If whichButtonDialogResult = DialogResult.Yes Then ' Code to clear the order. End If © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
20
Specifying a Default Button and Options
Use a different signature for the Message Box Show method to specify a default button Add the MessageBoxDefaultButton argument after the MessageBoxIcons argument Set message alignment with MessageBoxOptions argument © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
21
Input Validation Check to see if valid values were entered by user before beginning calculations Check for a range of values (reasonableness) If Integer.Parse(hoursTextBox.Text) > 10 Then MessageBox.Show("Too many hours") Check for a required field (not blank) If nameTextBox.Text <> "" Then ... © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
22
Performing Multiple Validations
Use nested If statement to validate multiple values on a form Examine example on Page 156 Use Case structure to validate multiple values Simpler and clearer than nested If No limit to number of statements that follow a Case statement When using a relational operator must use the word Is Use the word To to indicate a range of constants © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
23
Sharing an Event Procedure
Add events to the Handles clause at the top of an event procedure Allows the procedure to respond to events of other controls Key to using a shared event procedure is the sender argument Cast (convert) sender to a specific object type using the CType function © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
24
Calling Event Procedures
Reusable code General Form [Call] ProcedureName ( ) Keyword Call is optional and rarely used Examples Call clearButton_Click (sender, e) OR clearButton_Click (sender, e) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
25
Debugging (p 169) Debug Menu Debug Toolbar
Toggle BreakPoints on/off by clicking Editor's gray left margin indicator Step through Code, Step Into, Step Over View the values of properties, variables, mathematical expressions, and conditions © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
26
Debugging (cont.) Output Window Locals Window Autos Window
© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
27
Debug Menu and Toolbar © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
28
Writing to the Output Window
Debug.WriteLine(TextString) Debug.WriteLine(Object) Debug.WriteLine("calculateButton procedure entered") Debug.WriteLine(quantityTextBox) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
29
Breakpoints Toggle Breakpoints On/Off by clicking in Editor's gray left margin indicator © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
30
Checking the Current Value of Expressions
Place mouse pointer over variable or property to view current value © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
31
Locals Window Shows values of local variables that are within scope of current statement © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
32
Autos Window Automatically adjusts to show variables and properties that appear in previous and next few lines © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.