clearly visual basic: programming with visual basic 2008 chapter 8 what’s wrong with it?
TRANSCRIPT
![Page 1: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/1.jpg)
Clearly Visual Basic: Programming with Visual Basic 2008
Chapter 8 What’s Wrong with It?
![Page 2: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/2.jpg)
Objectives
• Locate syntax errors using the Error List window
• Locate a logic error by stepping through the code
• Locate logic errors using breakpoints
• Fix syntax and logic errors
Clearly Visual Basic: Programming with Visual Basic 2008 2
![Page 3: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/3.jpg)
There’s a Bug in my Soup!
• Downside to variables and named constants– Their use requires additional lines of code
• Bug– Error in a program’s code
• Debugging– Process of locating and correcting any bugs in a
program
• Program bugs – Typically caused by either syntax errors or logic
errors
Clearly Visual Basic: Programming with Visual Basic 2008 3
![Page 4: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/4.jpg)
Finding Syntax Errors
• Syntax error – Occurs when you break one of the language’s rules
• Code Editor– Detects most syntax errors as you enter instructions
Clearly Visual Basic: Programming with Visual Basic 2008 4
![Page 5: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/5.jpg)
Locating Logic Errors
• Logic error – Can occur for a variety of reasons– Difficult type of error to locate– Cannot be detected by Code Editor
Clearly Visual Basic: Programming with Visual Basic 2008 5
![Page 6: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/6.jpg)
Locating Logic Errors (continued)
• Debug the Discount application– Letter D at end of a value indicates value’s data type
is Decimal
Clearly Visual Basic: Programming with Visual Basic 2008 6
![Page 7: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/7.jpg)
Clearly Visual Basic: Programming with Visual Basic 2008 7
![Page 8: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/8.jpg)
Clearly Visual Basic: Programming with Visual Basic 2008
8
![Page 9: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/9.jpg)
Locating Logic Errors (continued)
• Use a breakpoint to pause execution at a specific line in the code
• Debug the Hours Worked application– .0 at end of a number indicates that number’s data
type is Double
Clearly Visual Basic: Programming with Visual Basic 2008 9
![Page 10: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/10.jpg)
Clearly Visual Basic: Programming with Visual Basic 2008 10
![Page 11: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/11.jpg)
Hawkins Solution
![Page 12: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/12.jpg)
Public Class frmMain Dim decBegin As Decimal Dim decEarned As Decimal Dim decSpent As Decimal Dim decEnding As DecimalPrivate Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click ' calculates the ending balance based on the beginning ' balance, amount earned, and amount spent ' assign input to variables Decimal.TryParse(txtBegin.Text, decBegin) Decimal.TryParse(txtSpent.Text, decSpent)
' calculate and display ending balance decEnding = decBegin + decEarned - decSpent lblEnding = decEnding.ToString("C2") End SubEnd Class
![Page 13: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/13.jpg)
' calculates and displays your weight on planets and the moon' Jupiter is 2.54 times earth weight, Venus is .91, Mars is .38, moon is .17
![Page 14: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/14.jpg)
Public Class frmMain Dim dblEarth As Double Dim dblJupiter As Double: Dim dblVenus As Double Dim dblMars As Double: Dim dblMoon As DoublePrivate Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click' calculates and displays your weight on planets and the moon' Jupiter is 2.54 times earth weight, Venus is .91, Mars is .38, moon is .17 ' calculate weights dblJupiter = dblEarth * 2.54 : dblVenus = dblEarth * 0.91 dblMars = dblEarth * 0.38 : dblMoon = dblEarth * 0.17 ' display weights lblJupiter.Text = dblJupiter.ToString("N2") lblVenus.Text = dblVenus.ToString("N2") lblMars.Text = dblMars.ToString("N2") lblMoon.Text = dblMars.ToString("N2")End Class
![Page 15: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/15.jpg)
Martins Solution
Answer should be $53.35
![Page 16: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/16.jpg)
16
Private Sub btnGainLoss_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGainLoss.Click ' calculates and displays the gain or loss on a stock
Dim intShares As Integer Dim decOpenPrice As Decimal Dim decClosePrice As Decimal Dim decGainLoss As Decimal
' assign input to variables Integer.TryParse(txtShares.Text, intShares) Decimal.TryParse(txtOpening.Text, decClosePrice) Decimal.TryParse(txtClosing.Text, decClosePrice)
' calculate and display gain or loss decGainLoss = decClosePrice - decOpenPrice * intShares lblGainLoss.Text = decGainLoss.ToString("C2")
End Sub
![Page 17: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/17.jpg)
Summary
• Program errors (bugs)– Caused by either syntax errors or logic errors
• Syntax errors in an application’s code – Listed in Error List window when you start the
application
• You can locate logic errors by stepping through the code
• Letter D at the end of a value– Indicates value’s data type is Decimal
Clearly Visual Basic: Programming with Visual Basic 2008 17
![Page 18: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?](https://reader036.vdocuments.site/reader036/viewer/2022062304/56649f445503460f94c65b99/html5/thumbnails/18.jpg)
Summary (continued)
• .0 at the end of a number – Indicates that the number’s data type is Double
• Before viewing value stored in a control or variable– First consider the value you expect to find
Clearly Visual Basic: Programming with Visual Basic 2008 18