Chapter 8 - Bobgill.com

January 7, 2018 | Author: Anonymous | Category: Engineering & Technology, Computer Science, Computer Programming
Share Embed Donate


Short Description

Download Chapter 8 - Bobgill.com...

Description

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

Clearly Visual Basic: Programming with Visual Basic 2008

2

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

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

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

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

Clearly Visual Basic: Programming with Visual Basic 2008

7

Clearly Visual Basic: Programming with Visual Basic 2008

8

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

Clearly Visual Basic: Programming with Visual Basic 2008

10

Hawkins Solution

Public Class frmMain Dim decBegin As Decimal Dim decEarned As Decimal Dim decSpent As Decimal Dim decEnding As Decimal Private 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

' 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

Public Class frmMain Dim dblEarth As Double Dim dblJupiter As Double: Dim dblVenus As Double Dim dblMars As Double: Dim dblMoon As Double Private 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

Martins Solution

Answer should be $53.35

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

16

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

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

View more...

Comments

Copyright � 2017 NANOPDF Inc.
SUPPORT NANOPDF