Debugging and Running
Debugging and Running
Running to a Point
Introduction
When executing a program, you can specify a section or line where you want the execution to pause, for any reason you judge necessary. This approach is useful if you have checked code up to a certain point and it looked alright. If you are not sure about code starting at a certain point, this can be your starting point.
To execute code up to a certain point
Practical Learning: Running to a Point
using System; namespace GeorgetownDryCleaningServices7 { public class CleaningOrder { public static int Main(string[] args) { // Price of items const double PriceOneShirt = 1.25; const double PriceAPairOfPants = 2.15; const double PriceOtherItem = 3.45; const double TaxRate = 5.75; // 5.75% // Basic information about an order string customerName, homePhone; DateTime orderDate = new DateTime(); DateTime orderTime = new DateTime(); // Unsigned numbers to represent cleaning items ushort numberOfShirts = 0, numberOfPants = 0, numberOfOtherItems = 0; // Each of these sub totals will be used for cleaning items double subTotalShirts, subTotalPants, subTotalOtherItems; // Values used to process an order double totalOrder, taxAmount, salesTotal; double amountTended = 0, moneyChange; Console.Title = "Georgetown Dry Cleaning Services"; Console.WriteLine("==============================================="); Console.WriteLine("-/- Georgetown Dry Cleaning Services -/-"); Console.WriteLine("==============================================="); // Request order information from the user Console.Write("Enter Customer Name: "); customerName = Console.ReadLine(); Console.Write("Enter Customer Phone: "); homePhone = Console.ReadLine(); Console.Write("Enter the order date (mm/dd/yyyy): "); try { orderDate = DateTime.Parse(Console.ReadLine()); } catch (FormatException fexc) { Console.BackgroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(fexc.Message); } Console.ResetColor(); Console.Write("Enter the order time (HH:MM): "); try { orderTime = DateTime.Parse(Console.ReadLine()); } catch (FormatException fexc) { Console.BackgroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(fexc.Message); } Console.ResetColor(); // Request the quantity of each category of items try { Console.Write("Number of Shirts: "); numberOfShirts = ushort.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("The value you typed for the number of " + "shirts is not a valid number."); } try { Console.Write("Number of Pants: "); numberOfPants = ushort.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("The value you typed for the number of " + "pants is not a valid number."); } try { Console.Write("Number of Other Items: "); numberOfOtherItems = ushort.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("The value you typed for the number of " + "other types of items is not a valid number."); } // Perform the necessary calculations subTotalShirts = numberOfShirts * PriceOneShirt; subTotalPants = numberOfPants * PriceAPairOfPants; subTotalOtherItems = numberOfOtherItems * PriceOtherItem; // Calculate the "temporary" total of the order totalOrder = subTotalShirts + subTotalPants + subTotalOtherItems; // Calculate the tax amount using a constant rate taxAmount = totalOrder * TaxRate / 100.00; // Add the tax amount to the total order salesTotal = totalOrder + taxAmount; Console.WriteLine("----------------------------------------------"); // Communicate the total to the user... Console.WriteLine("The Total order is: {0:C}", salesTotal); // and request money for the order try { Console.Write("Amount Tended? "); amountTended = double.Parse(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("You were asked to enter an amount of money but something bad happened."); } // Calculate the difference owed to the customer // or that the customer still owes to the store moneyChange = amountTended - salesTotal; Console.Clear(); Console.Title = "Georgetown Dry Cleaning Services"; // Display the receipt Console.WriteLine("========================================="); Console.WriteLine("-/- Georgetown Dry Cleaning Services -/-"); Console.WriteLine("========================================="); Console.WriteLine("Customer: {0}", customerName); Console.WriteLine("Home Phone: {0}", homePhone); Console.WriteLine("Order Date: {0:D}", orderDate); Console.WriteLine("Order Time: {0}", orderTime.ToShortTimeString()); Console.WriteLine("-----------------------------------------"); Console.WriteLine("Item Type Qty Unit/Price Sub-Total"); Console.WriteLine("-----------------------------------------"); Console.WriteLine("Shirts{0,9} {1:C} {2:C}", numberOfShirts, PriceOneShirt, subTotalShirts); Console.WriteLine("Pants{0,10} {1:C} {2,6}", numberOfPants, PriceAPairOfPants, subTotalPants.ToString("C")); Console.WriteLine("Other Items{0,4} {1:C} {2:C}", numberOfOtherItems, PriceOtherItem, subTotalOtherItems); Console.WriteLine("-----------------------------------------"); Console.WriteLine("Total Order: {0:C}", totalOrder); Console.WriteLine("Tax Rate: {0:P}", TaxRate / 100); Console.WriteLine("Tax Amount: {0:C}", taxAmount); Console.WriteLine("Net Price: {0:C}", salesTotal); Console.WriteLine("-----------------------------------------"); Console.WriteLine("Amount Tended: {0:C}", amountTended); Console.WriteLine("Difference: {0:C}", moneyChange); Console.WriteLine("========================================="); Console.ReadKey(); return 0; } } }
Customer Phone: | 301-208-2333 |
Order Date: | 10/08/2019 |
Order Time: | 08:14 AM |
Number of Shirts: | 5 |
Number of Pants: | 2 |
Number of Other Items: | 8 |
========================================= -/- Georgetown Dry Cleaning Services -/- ========================================= Customer: Steve Longley Home Phone: 301-208-2333 Order Date: Tuesday, October 08, 2019 Order Time: 8:14 AM ----------------------------------------- Item Type Qty Unit/Price Sub-Total ----------------------------------------- Shirts 5 $1.25 $6.25 Pants 2 $2.15 $4.30 Other Items 8 $3.45 $27.60 ----------------------------------------- Total Order: $38.15 Tax Rate: 5.75 % Tax Amount: $2.19 Net Price: $40.34 ----------------------------------------- Amount Tended: $60.00 Difference: $19.66 =========================================
Breakpoints
A breakpoint on a line is the code where you want the exection to suspend. You must explicitly specify that line by creating a breakpoint. You can as well create as many breakpoints as you want. Also, at any time, you can remove a breakpoint you don't need anymore.
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. 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 .
After using a breakpoint, you can remove it. To delete a breakpoint:
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.
Practical Learning: Using Breakpoints
Customer Name: | Hermine Simms |
Customer Phone: | 410-573-2031 |
Order Date: | 10/08/2019 |
Order Time: | 09:22 AM |
Number of Shirts: | 3 |
Number of Pants: | 3 |
Number of Other Items: | 12 |
Customer Name: | Ginette Rhoads |
Customer Phone: | 301-217-9494 |
Order Date: | 11/06/2019 |
Order Time: | 02:07 PM |
Number of Shirts: | 0 |
Number of Pants: | 2 |
Number of Other Items: | 0 |
Stepping to Breakpoints
You can combine the Step Into and/or the Step Over features 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
========================================= -/- Georgetown Dry Cleaning Services -/- ========================================= Customer: James Sandt Home Phone: 301-870-7454 Order Date: Thursday, October 10, 2019 Order Time: 6:02 PM ----------------------------------------- Item Type Qty Unit/Price Sub-Total ----------------------------------------- Shirts 8 $1.25 $10.00 Pants 2 $2.15 $4.30 Other Items 12 $3.45 $41.40 ----------------------------------------- Total Order: $55.70 Tax Rate: 5.75 % Tax Amount: $3.20 Net Price: $58.90 ----------------------------------------- Amount Tended: $60.00 Difference: $1.10 =========================================
Debugging and 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
using System; namespace Exercise1 { public class Exercise { public void Display() { Console.WriteLine("Welcome to the wonderfule world of C#!"); } } }
Debugging and the Main Function of a Program
As you know already, the starting point of a C# application is the Main() method (in C#, that would be a class that contains a method named Main). If you start debugging an application that uses many code files (files that contain classes), the debugger must first identify the file that contains the Main() method. If you are debugging a regular console application but the debugger cannot find a file that contains the Main() method, you would receive an error, indicating that your project does not contain an entry point.
Practical Learning: Checking the Presence of a Main Method
Severity Code Description Project File Line Suppression State Error CS5001 Program does not contain a static 'Main' method suitable for an entry point Exercise1 C:\Users\pkatts\Source\Repos\Consoles\Exercise1\Exercise1\CSC 1 Active
Severity Code Description Project File Line Suppression State Error CS5001 Program does not contain a static 'Main' method suitable for an entry point Exercise1 C:\Users\pkatts\Source\Repos\Consoles\Exercise1\Exercise1\CSC 1 Active
Using Separate Files
Instead of just one class or one file, we have learned, and will continue learning, that, to organize your project, you can create the classes of your project in different files. When you debug a project that uses different classes or files that contain classes, the debugger is aware of the objects and where they are created. As we will learn later, there are various windows that assist you with identifying the objects of your project. As a result, the tools such as the Locals window display their contents accordingly.
When debugging a project that uses many code files or contains many classes, when the debugging focuses on a certain variable, if the variable is a structure or a class, an arrow icon appears to its left, indicating that it has members that are fields or properties. Here is an example:
In this case, to show the variables, that is, to expand the node, click the arrow icon. This would show the fields under the variable name and the name of the class between curly brackets under the Value column:
Practical Learning: Introducing Class Debugging
namespace WattsALoan3 { public class Customer { public string FullName { get; set; } public string PhoneNumber { get; set; } public Customer(string name = "John Doe", string phone = "000-000-0000") { FullName = name; PhoneNumber = phone; } } }
namespace WattsALoan3 { public class Employee { public long EmployeeNumber { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Title { get; set; } 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 WattsALoan3 { public class LoanInformation { public double Principal { get; set; } public double InterestRate { get; set; } public double Period { get; set; } public double InterestAmount { get; set; } public double FutureValue { get; set; } } }
using static System.Console; namespace WattsALoan3 { 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("======================================"); } } }
using static System.Console; namespace WattsALoan3 { public class WattsALoan { public static int Main(string[] args) { LoanEvaluation evaluation = new LoanEvaluation(); Title = "Watts A Loan?"; WriteLine("This application allows you to evaluate a loan"); evaluation.IdentifyEmployee(); evaluation.IdentifyCustomer(); Clear(); evaluation.GetLoanValues(); Clear(); evaluation.Show(); return 0; } } }
====================================== Loan Summary =------------------------------------= Prepared by: 20584 - Mahmouda, Joshua Accounts Representative =------------------------------------= Prepared for: Lauren Sachs (301) 438-6243 =------------------------------------= Principal: 24750.00 Interest Rate: 11.35 % Period For: 60 months Interest Amount: 14045.63 Future Value: 38795.63 ======================================
|
||
Previous | Copyright © 2010-2019, FunctionX | Next |
|