1 / 18

Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 8 What’s Wrong with It?. 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.

felix-combs
Download Presentation

Clearly Visual Basic: Programming with Visual Basic 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 8 What’s Wrong with It?

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Clearly Visual Basic: Programming with Visual Basic 2008

  8. Clearly Visual Basic: Programming with Visual Basic 2008

  9. 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

  10. Clearly Visual Basic: Programming with Visual Basic 2008

  11. Hawkins Solution

  12. 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 Sub End Class

  13. ' 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

  14. 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

  15. Martins Solution Answer should be $53.35

  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

  17. 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

  18. 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

More Related