murach’s visual basic 2008, c7, modified or added© 2008, mike murach & associates, inc. slide 1

46
Murach’s Visual Basic 2008, C7, modified or added © 2008, Mike Murach & Associates, Inc. Slide 1 C hapter7 How to handle exceptions and validate data

Upload: myra-griffith

Post on 20-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Murach’s Visual Basic 2008, C7, modified or added© 2008, Mike Murach & Associates, Inc. Slide 3

TRANSCRIPT

Slide 1© 2008, Mike Murach & Associates, Inc.
Slide 1
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
Slide 2
© 2008, Mike Murach & Associates, Inc.
Slide 3
Name the text box,
© 2008, Mike Murach & Associates, Inc.
Slide 4
© 2008, Mike Murach & Associates, Inc.
Slide 5
© 2008, Mike Murach & Associates, Inc.
Slide 6
© 2008, Mike Murach & Associates, Inc.
Slide 7
© 2008, Mike Murach & Associates, Inc.
Slide 8
© 2008, Mike Murach & Associates, Inc.
Slide 9
© 2008, Mike Murach & Associates, Inc.
Slide 10
© 2008, Mike Murach & Associates, Inc.
Slide 11
Properties
Methods
© 2008, Mike Murach & Associates, Inc.
12
© 2008, Mike Murach & Associates, Inc.
Slide 13
© 2008, Mike Murach & Associates, Inc.
Slide 14
© 2008, Mike Murach & Associates, Inc.
Slide 15
Practice Exercise
Create a project that will allow you to test the code on Slide 14.
The code should be activated when a button is clicked.
What is accomplished by the last argument of MessageBox.Show, ex.GetType.ToString?
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
16
© 2008, Mike Murach & Associates, Inc.
Slide 17
© 2008, Mike Murach & Associates, Inc.
Slide 18
© 2008, Mike Murach & Associates, Inc.
Slide 19
CInt(10.5) rounds to 10 – always rounds
Convert.ToInt32(10.5) – throws a Format Exception
Which do you want?
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
20
© 2008, Mike Murach & Associates, Inc.
Slide 21
© 2008, Mike Murach & Associates, Inc.
Slide 22
© 2008, Mike Murach & Associates, Inc.
Slide 23
© 2008, Mike Murach & Associates, Inc.
Slide 24
© 2008, Mike Murach & Associates, Inc.
Slide 25
© 2008, Mike Murach & Associates, Inc.
Slide 26
© 2008, Mike Murach & Associates, Inc.
Slide 27
© 2008, Mike Murach & Associates, Inc.
Slide 28
© 2008, Mike Murach & Associates, Inc.
Slide 29
© 2008, Mike Murach & Associates, Inc.
Slide 30
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
31
Did the entry convert to the required data type?
Was the entry within the required range?
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
32
© 2008, Mike Murach & Associates, Inc.
Slide 33
© 2008, Mike Murach & Associates, Inc.
Slide 34
© 2008, Mike Murach & Associates, Inc.
Slide 35
© 2008, Mike Murach & Associates, Inc.
Slide 36
Code for all three common checks for every textbox
Is there an easier method?
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
37
© 2008, Mike Murach & Associates, Inc.
Slide 38
© 2008, Mike Murach & Associates, Inc.
Slide 39
© 2008, Mike Murach & Associates, Inc.
Slide 40
© 2008, Mike Murach & Associates, Inc.
Slide 41
© 2008, Mike Murach & Associates, Inc.
Slide 42
© 2008, Mike Murach & Associates, Inc.
Slide 43
© 2008, Mike Murach & Associates, Inc.
Slide 44
© 2008, Mike Murach & Associates, Inc.
Slide 45
To Learn More:
Do p. 221 Exercise 7-1 #1-7 (In #5, use only a single Return statement.)
Do p. 222 Exercise 7-2 #1-7
Murach’s Visual Basic 2008, C7, modified or added
© 2008, Mike Murach & Associates, Inc.
46
The syntax of the IsNumeric function
IsNumeric(expression)
An IsNumeric function used for data validation
An IsNumeric function used for data validation The syntax of the IsNumeric function
IsNumeric(expression)
The syntax for displaying a dialog box with an OK
button
MessageBox.Show( _
"Entry Error")
The dialog box for the state ment above
The syntax for displaying a dialog box with an OK button
MessageBox.Show(text[, caption])
MessageBox.Show( _
"Entry Error")
MessageBox.Show( _
"Program Error")
MessageBox.Show( _
"Program Error")
Constants that you can use for new lines and tabs
Description ControlChars member VB constant
Carriage return character ControlChars.CrLf vbCrLf
Carriage return character ControlChars.NewLine vbNewLine
Tab character ControlChars.Tab vbTab
Constants that you can use for new lines and tabs
Description ControlChars member VB constant
Carriage return character ControlChars.CrLf vbCrLf
Carriage return character ControlChars.NewLine vbNewLine
Tab character ControlChars.Tab vbTab
The Exception hierarchy for five common
exceptions
text
Try
trystatements
Catch
catchstatements
& "Subtotal field.", "Entry Error")
Try
trystatements
Catch
catchstatements
& "Subtotal field.", "Entry Error")
occurs
The dialog box that’s displayed if an exception occurs
The syntax for a Try...Catch statement that
accesses the exception
The syntax for a Try...Catch statement that accesses the exception
Try
trystatements
Property Description
exception.
StackTrace Gets a string that lists the procedures that were
called before the exception occurred.
A common method for all ex ceptions
Method Description
Two common properties for all exceptions
Property Description
Message Gets a message that briefly describes the current exception.
StackTrace Gets a string that lists the procedures that were called before the exception occurred.
A common method for all exceptions
Method Description
A Try...Catch statement that ac cesses the
exception
Dim subtotal As Decimal
The dialog box that’s displayed if an exception occurs
The complete syntax for the Try...Catch statement
Try
Trystatements
catchstatements
[Finally
statements]
Try
Trystatements
text
exceptions
Try
monthlyInvestment = _
Convert.ToDecimal(txtMonthlyInvestment.Text)
yearlyInterestRate = _
Convert.ToDecimal(txtInterestRate.Text)
MessageBox.Show( _
& "Please check all entries.", _
MessageBox.Show( _
& "Please enter smaller values.", _
MessageBox.Show(ex.Message, ex.GetType.ToString)
PerformCleanup()
Try
monthlyInvestment = _
Convert.ToDecimal(txtMonthlyInvestment.Text)
yearlyInterestRate = _
Convert.ToDecimal(txtInterestRate.Text)
MessageBox.Show( _
& "Please check all entries.", _
MessageBox.Show( _
& "Please enter smaller values.", _
MessageBox.Show(ex.Message, ex.GetType.ToString)
PerformCleanup()
exceptions (continued)
PerformCleanup()
Finally
PerformCleanup()
Throw New exceptionclass ([message])
Throw exceptionName
Throw New exceptionclass([message])
Throw exceptionName
FormatException -- Why needed?
Private Function FutureValue( _
monthlyInvestment As Decimal, _
As Decimal
End If
End If
Private Function FutureValue( _
monthlyInvestment As Decimal, _
As Decimal
End If
End If
purposes
Try
Try
Try
Convert.ToDecimal(txtSubtotal.Text)
Try
Convert.ToDecimal(txtSubtotal.Text)
An exceptional situation: Procedure cannot complete its task
Desire to test an exception handler
And
An exceptional situation: Procedure cannot complete its task
Desire to test an exception handler
And
A situation where exception processing needs to be performed in
parts
do more processing elsewhere
When to throw an exception (cont.)
A situation where exception processing needs to be performed in parts
catch the exception,
perform some processing,
exception handling
Dim futureValue As Decimal = Me.FutureValue( _
monthlyInvestment, monthlyInterestRate, months)
The code for the Future Value application with exception handling
Private Sub btnCalculate_Click( _
Dim futureValue As Decimal = Me.FutureValue( _
monthlyInvestment, monthlyInterestRate, months)
exception handling (continued)
"Entry Error")
txtMonthl yInvestment.Select()
End Try
End Sub
The code for the Future Value application with exception handling (continued)
Catch ex As FormatException
"Entry Error")
exception handling (continued)
Private Function FutureValue( _
FutureValue = (FutureValue + monthlyInvestment) _
End Function
The code for the Future Value application with exception handling (continued)
Private Function FutureValue( _
FutureValue = (FutureValue + monthlyInvestment) _
If txtMonthlyInvestment.Text = "" Then
"Entry Error")
If txtMonthlyInvestment.Text = "" Then
"Entry Error")
format
Try
Convert.ToDecimal(txtMonthlyInvestment.Text)
"Entry Error")
Code that checks an entry for a valid decimal format
Try
Convert.ToDecimal(txtMonthlyInvestment.Text)
"Entry Error")
Dim monthlyInvestment As Decimal = _
"Entry Error")
"Entry Error")
txtMonthlyInvestment.Sele ct( _
0, txtMonthlyInvestment.Text.Length)
End If
Dim monthlyInvestment As Decimal = _
"Entry Error")
"Entry Error")
The syntax of the Select method for a text box
Select(startingindex, numberofcharacters )
Textbox.Text.Length
of characters of the user entered in the textbox
The syntax of the Select method for a text box
Select(startingindex, numberofcharacters)
Textbox.Text.Length
Length (property of the Text property): the number of characters of the user entered in the textbox
A Function procedure that checks for a required
field
ByVal name As String) _
"Entry Error")
Private Function IsPresent(ByVal textBox As TextBox, _
ByVal name As String) _
"Entry Error")
Decimal format
ByVal name As String) _
MessageBox.Show( _
"Entry Error")
textBox.Select(0, textBox.Text.Length)
IsDecimal = False
End Try
Return IsDecimal
End Function
A Function procedure that checks for a valid Decimal format
Private Function IsDecimal(ByVal textBox As TextBox, _
ByVal name As String) _
"Entry Error")
textBox.Select(0, textBox.Text.Length)
IsDecimal = False
End Try
Return IsDecimal
End Function
numeric range
ByVal max As Decimal) As Boolean
Dim number As Decimal = CDec(textB ox.Text)
If number < min OrElse number > max Then
MessageBox.Show(name & " must be between " _
A Function procedure that checks for a valid numeric range
Private Function IsWithinRange( _
ByVal max As Decimal) As Boolean
Dim number As Decimal = CDec(textBox.Text)
If number < min OrElse number > max Then
MessageBox.Show(name & " must be between " _
validity of one entry
If IsPresent(txtMonthlyInvestment, "Monthly Investment") _
End If
Code that uses the three procedures to check the validity of one entry
If IsPresent(txtMonthlyInvestment, "Monthly Investment") _
End If
Private Function IsValidData() As Boolean
' Validate the Monthly Investment text box
If Not IsPresent(txtMonthlyInvestment, _
"Monthly Investment") Then
Return False
End If
Private Function IsValidData() As Boolean
' Validate the Monthly Investment text box
If Not IsPresent(txtMonthlyInvestment, _
"Monthly Investment") Then
Return False
End If
Do NOT use this method …A series of simple if
statements (continued)
If Not IsPresent(txtInterestRate, _
Return False
End If
Do NOT use this method…A series of simple if statements (continued)
' Validate the Interest Rate text box
If Not IsPresent(txtInterestRate, _
Return False
End If
(continued)
If Not IsPresent(txtYears, "Number of Years") Then
Return False
End If
Return False
End If
Return False
End If
End Function
Do NOT use…A series of simple if statements (continued)
' Validate the Years text box
If Not IsPresent(txtYears, "Number of Years") Then
Return False
End If
Return False
End If
Return False
End If
End Function
return statement to validate multiple entries
Private Function IsValidData() As Boolean
Return _
IsPresent(txtMonthlyInvestment, _
IsPresent(txtInterestRate, _
IsPresent(txtYears, "Number of Years") AndAlso _
IsInt32(txtYears, "Number of Years") AndAlso _
IsWithinRange(txtYears, "Number of Years", 1, 50)
End Function
Code that uses compound conditions in a single return statement to validate multiple entries
Private Function IsValidData() As Boolean
Return _
IsPresent(txtMonthlyInvestment, _
IsPresent(txtInterestRate, _
IsPresent(txtYears, "Number of Years") AndAlso _
IsInt32(txtYears, "Number of Years") AndAlso _
IsWithinRange(txtYears, "Number of Years", 1, 50)
End Function