Debugging with Breakpoints
Debugging with Breakpoints
Introduction
A breakpoint is a line in the code where you want the execution to suspend. The reason you use a breakpoint is to examine what happens in your application when execution reaches that point.
Practical Learning: Introducing Errors
using static System.Console; double principal; double interestRate; double period; double interestAmount; double futureValue; Title = "Watts A Loan?"; WriteLine("This application allows you to evaluate a loan"); principal = 6585; interestRate = 9.725 / 100; period = 36 / 12; interestAmount = principal * interestRate * period; futureValue = principal + interestAmount; Clear(); WriteLine("============================"); WriteLine("Loan Summary"); WriteLine("=--------------------------="); WriteLine("Principal: {0:F}", principal); WriteLine("Interest Rate: {0:P}", interestRate); WriteLine("Period For: {0} months", period * 12); WriteLine("Interest Amount: {0:F}", interestAmount); WriteLine("Future Value: {0:F}", futureValue); WriteLine("============================");
Creating a Breakpoint
A breakpoint is not part of your code; it is just a section you as the developer (or application tester) explicitly specify as a breakpoint. This means that, in order to get a breakpoint, you must create it. To create a breakpoint, first identify the line of code where you want to add it. Then:
A breakpoint is represented by a red circular button:

In the same way, you can create as many breakpoints as you want.
After creating a breakpoint, when code executes and reaches that line, it would pause and let you know by drawing a right-pointing yellow button:

Practical Learning: Using Breakpoints

============================ Loan Summary =--------------------------= Principal: 6255.75 Interest Rate: 9.73% Period For: 48 months Interest Amount: 2433.49 Future Value: 8689.24 ============================ To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

============================ Loan Summary =--------------------------= Principal: 18225.45 Interest Rate: 6.82% Period For: 60 months Interest Amount: 6210.32 Future Value: 24435.77 ============================ To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
Stepping to Breakpoints
You can combine the Step Into and/or the Step Over feature with breakpoints. That is, you can examine each code line after line until you get to a specific line. This allows you to monitor the values of variables and see their respective values up to a critical section. To do this, first create one or more breakpoints, then proceed with steps of your choice.
Practical Learning: Stepping to Breakpoints
============================ Loan Summary =--------------------------= Principal: 6535.85 Interest Rate: 7.62% Period For: 48 months Interest Amount: 1993.43 Future Value: 8529.28 ============================ To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
A Breakpoints Window
Introduction
Breakpoints are some types of objects and must be managed. For example you can create breakpoints as we saw in previous sections. You can also delete them or create new ones in different sections of your code.
A Window for Breakpoints
To assist you in using and managing breakpoints, Microsoft Visual Studio provides a special window: The Breakpoints window. To get the breackpoint window, you should first start a debugging operation. The Breakpoint window usually appears in the bottom (-right) section of Microsoft Visual Studio.
When in a Breakpoint-Less Session
When you starting a debugging session, if there is no breakpoint in your project, a Breakpoints window would display but it would be empty:

Otherwise, if you had first created some breakpoints, their list would display in the Breakpoints window.
Managing Breakpoints
Introduction
Remember that each breakpoint shows a button in the margin of its line in the Code Editor. In the Breakpoints window, each breakpoint is represented by a row in the list:

Removing a Breakpoint
At any time, such as after using a breakpoint, you can remove it. To delete a breakpoint:


Deleting all Breakpoinst
Remember that you can create more than one breakpoint. If you have more than one breakpoint in your code, execution would pause at each one of them. At any time, you can remove one or all breakpoints. To delete all breakpoints, on the main menu, click Debug -> Delete all Breakpoints.
Disabling a Breakpoint
Remember that when you are debugging your code, if you had created some breakpoints, code execution would stop (or break) at each breakpoint. If you already what is supposed to happen on a breakpoint, we saw that you can delete such a breakpoint. Sometimes, you may want to conduct a debugging operation that must ignore a certain breakpoint, in which case you don't yet want to remove such a breakpoint. In that case, you can disable a breakpoint instead of deleting it.
To disable a breakpoint:
Introduction
A breakpoint is a line in the code where you want the execution to suspend. The reason you use a breakpoint is to examine what happens in your application when execution reaches that point.
Creating a Breakpoint
A breakpoint is not part of your code; it is just a section you as the developer (or application tester) explicitly specify as a breakpoint. This means that, in order to get a breakpoint, you must create it. To create a breakpoint, first identify the line of code where you want to add it. Then:
A breakpoint is represented by a red circular button:

In the same way, you can create as many breakpoints as you want.
After creating a breakpoint, when code executes and reaches that line, it would pause and let you know by drawing a right-pointing yellow button:

Practical Learning: Using Breakpoints

============================ Loan Summary =--------------------------= Principal: 6255.75 Interest Rate: 9.73% Period For: 48 months Interest Amount: 2433.49 Future Value: 8689.24 ============================ To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

============================ Loan Summary =--------------------------= Principal: 18225.45 Interest Rate: 6.82% Period For: 60 months Interest Amount: 6210.32 Future Value: 24435.77 ============================ To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
Stepping to Breakpoints
You can combine the Step Into and/or the Step Over feature with breakpoints. That is, you can examine each code line after line until you get to a specific line. This allows you to monitor the values of variables and see their respective values up to a critical section. To do this, first create one or more breakpoints, then proceed with steps of your choice.
Practical Learning: Stepping to Breakpoints
============================ Loan Summary =--------------------------= Principal: 6535.85 Interest Rate: 7.62% Period For: 48 months Interest Amount: 1993.43 Future Value: 8529.28 ============================ To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
A Breakpoints Window
Introduction
Breakpoints are some types of objects and must be managed. For example you can create breakpoints as we saw in previous sections. You can also delete them or create new ones in different sections of your code.
A Window for Breakpoints
To assist you in using and managing breakpoints, Microsoft Visual Studio provides a special window: The Breakpoints window. To get the breackpoint window, you should first start a debugging operation. The Breakpoint window usually appears in the bottom (-right) section of Microsoft Visual Studio.
When in a Breakpoint-Less Session
When you starting a debugging session, if there is no breakpoint in your project, a Breakpoints window would display but it would be empty:

Otherwise, if you had first created some breakpoints, their list would display in the Breakpoints window.
Managing Breakpoints
Introduction
Remember that each breakpoint shows a button in the margin of its line in the Code Editor. In the Breakpoints window, each breakpoint is represented by a row in the list:

Removing a Breakpoint
At any time, such as after using a breakpoint, you can remove it. To delete a breakpoint:


Disabling a Breakpoint
Remember that when you are debugging your code, if you had created some breakpoints, code execution would stop (or break) at each breakpoint. If you already what is supposed to happen on a breakpoint, we saw that you can delete such a breakpoint. Sometimes, you may want to conduct a debugging operation that must ignore a certain breakpoint, in which case you don't yet want to remove such a breakpoint. In that case, you can disable a breakpoint instead of deleting it.
To disable a breakpoint:


Deleting all Breakpoinst
Remember that you can create more than one breakpoint. If you have more than one breakpoint in your code, execution would pause at each one of them. At any time, you can remove one or all breakpoints. To delete all breakpoints, on the main menu, click Debug -> Delete all Breakpoints.
Debugging Separate Files
Introduction
As you know already, a program can use many files, some files come from the .NET Framework, some are created by Microsoft Visual Studio when you start a project, and you create the others as you judge them necessary for your project. As a result, when debugging, you can consider files that are linked at one time or another. The process of debugging is primarily the same. You just have to keep in mind that you are dealing with many classes and probably different files. This has some consequences on the results you see.
Practical Learning: Debugging With a Class
namespace WattsALoan02.Models
{
internal class Customer
{
public string fullName;
public string phoneNumber;
public Customer(string name = "John Doe", string phone = "000-000-0000")
{
fullName = name;
phoneNumber = phone;
}
}
}namespace WattsALoan02.Models
{
internal class Employee
{
public long employeeNumber;
public string firstName;
public string lastName;
public string title;
public Employee(long emplNbr = 0,
string fName = "Unknown",
string lName = " Not Specified",
string position = "Loan Specialist")
{
lastName = lName;
firstName = fName;
employeeNumber = emplNbr;
title = position;
}
public string GetEmployeeName()
{
return lastName + ", " + firstName;
}
}
}namespace WattsALoan02.Models
{
internal class LoanInformation
{
public double principal;
public double interestRate;
public double period;
public double interestAmount;
public double futureValue;
}
}using WattsALoan.Models;
using static System.Console;
public class LoanEvaluation
{
private Employee clerk;
private Customer client;
private LoanInformation loan;
public LoanEvaluation()
{
clerk = new Employee();
client = new Customer();
loan = new LoanInformation();
}
public void IdentifyEmployee()
{
WriteLine("Enter the following pieces of information " +
"about the employee who prepared this loan.");
Write("Employee #: ");
clerk.employeeNumber = long.Parse(ReadLine()!);
Write("First Name: ");
clerk.firstName = ReadLine()!;
Write("Last Name: ");
clerk.lastName = ReadLine()!;
Write("Title: ");
clerk.title = ReadLine()!;
}
public void IdentifyCustomer()
{
WriteLine("Enter the following pieces of information " +
"about the customer for whom this loan was prepared.");
Write("Customer Name: ");
client.fullName = ReadLine()!;
Write("Phone Number: ");
client.phoneNumber = ReadLine()!;
}
public void GetLoanValues()
{
WriteLine("Enter the following pieces of information " +
"about the values used for the loan.");
Write("Enter the principal: ");
loan.principal = double.Parse(ReadLine()!);
Write("Enter the interest rate: ");
loan.interestRate = double.Parse(ReadLine()!) / 100;
Write("Enter the number of months: ");
loan.period = double.Parse(ReadLine()!) / 12;
}
public void Show()
{
loan.InterestAmount = loan.principal * loan.interestRate * loan.period;
loan.FutureValue = loan.principal + loan.interestAmount;
WriteLine("======================================");
WriteLine("Loan Summary");
WriteLine("=------------------------------------=");
WriteLine("Prepared by: {0} - {1}\n {2}",
clerk.employeeNumber,
clerk.GetEmployeeName(), clerk.title);
WriteLine("=------------------------------------=");
WriteLine("Prepared for: {0}\n {1}",
client.fullName, client.phoneNumber);
WriteLine("=------------------------------------=");
WriteLine("Principal: {0:F}", loan.principal);
WriteLine("Interest Rate: {0:P}", loan.interestRate);
WriteLine("Period For: {0} months", loan.period * 12);
WriteLine("Interest Amount: {0:F}", loan.interestAmount);
WriteLine("Future Value: {0:F}", loan.futureValue);
WriteLine("======================================");
}
}|
|
|||
| Previous | Copyright © 2010-2026, FunctionX | Monday 10 November 2025, 10:56 | Next |
|
|
|||