Debugging an Object
Debugging an Object
Practical Learning: Introducing Errors
.
Practical Learning: Introducing Function Debugging
The Step Into feature is a good tool to monitor the behavior of variables inside a function. This also allows you to know if a function is behaving as expected. Once you have established that a function is alright, you may want to skip it. Instead of executing one line at a time, the debugger allows you to execute a whole function at a time or to execute the lines in some functions while skipping the others. To support this, you use a feature named Step Over.
To step over a function, while debugging:
As its name suggests, the Step Over feature allows you to skip a method if you know it doesn't have any problem. When debugging, you choose what methods to step into and which ones to step over.
Practical Learning: Stepping Over
Debugging an Object
Introduction
Some of your applications will have objects (from a structure, a record, or a class). Here is an example:
using static System.Console;
Square sqr = new();
Rectangle rect = new(496.84, 825.97);
sqr.Side = 927.93;
WriteLine("Geometry - Square");
WriteLine("----------------------------");
WriteLine($"Side: {sqr.Side}");
WriteLine($"Perimeter: {sqr.Perimeter}");
WriteLine($"Area: {sqr.Area}");
WriteLine("============================");
WriteLine("Geometry - Rectangle");
WriteLine("----------------------------");
WriteLine($"Width: {rect.Width}");
WriteLine($"Height: {rect.Height}");
WriteLine($"Perimeter: {rect.CalculatePerimeter()}");
WriteLine($"Area: {rect.CalculateArea()}");
WriteLine("============================");
// A Structure
internal record struct Square
{
public double Side { get; set; }
public double Perimeter
{
get
{
return Side * 4.00;
}
}
public double Area
{
get
{
return Side * Side;
}
}
}
public class Rectangle
{
public Rectangle(double width, double height)
{
(Width, Height) = (width, height);
}
public double Width { get; init; }
public double Height { init; get; }
public double CalculatePerimeter()
{
return (Width * Height) * 2.00;
}
public double CalculateArea()
{
return Width * Width;
}
}
When debugging involves an object, the Locals window includes a record for an object. Such a record displays a right-pointing button on the left of the name of the object. Here is an example:
In this case, to show the variables, you can expand the node. To do this, you can 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:
The Type of a Variable
The Type column shows the data type of the variable. If the variable is an object (from a class, a record, a structure, or an interface), the name of the type shows in the Type column.
Debugging Fundamentals
Practical Learning: Examining Local Variables
============================ Loan Summary =--------------------------= Principal: 6500.00 Interest Rate: 12.65 % Period For: 36 months Interest Amount: 2466.75 Future Value: 8966.75 ============================
Running to a Point
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
| Principal: | 21466.85 |
| Interest Rate: | 12.25 |
| Number of Months: | 60 |
============================ Loan Summary =--------------------------= Principal: 21466.85 Interest Rate: 12.25% Period For: 60 months Interest Amount: 13148.45 Future Value: 34615.30 ============================ 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 . . .
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.
As you know already, the starting point of a C# application is the primary document that displays when you have created a Console App project (in previous C# console applications, 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 primary document of the project (if you are debugging a normal console application but the debugger cannot find a file that contains the Main() function, you would receive an error, indicating that your project does not contain an entry point).
Practical Learning: Debugging Classes
using WattsALoan02.Models;
using static System.Console;
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();
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("======================================");
}
}



Practical Learning: Using the Immediate Window
double grossPay = 0D;
double timeWorked = 0D;
double hourlySalary = 0.00D;
// double federalIncomeTax = 0.00D;
// const double FICATaxRate = 0.0765; // 7.65%
// double FICATaxAmount = 0.00D;
double totalDeductions = 0.0D;
double netPay = 0.00D;
Title = "Payroll Evaluation";
/*
WriteLine("To test a payroll, enter the following pieces of information");
Write("Time Wored: ");
timeWorked = double.Parse(ReadLine());
Write("Hourly Salary: ");
hourlySalary = double.Parse(ReadLine());
Write("Enter the amount of the deductions: ");
totalDeductions = double.Parse(ReadLine());
*/
grossPay = hourlySalary * timeWorked;
netPay = grossPay - totalDeductions;
Clear();
WriteLine("=======================");
WriteLine("Payroll Evaluation");
WriteLine("-----------------------");
WriteLine("Time Worked: {0, 7}", timeWorked.ToString("F"));
WriteLine("Hourly Salary: {0, 7}", hourlySalary.ToString("F"));
WriteLine("Gross Pay: {0, 7}", grossPay.ToString("F"));
WriteLine("Deductions: {0, 7}", totalDeductions.ToString("F"));
WriteLine("Net Pay: {0, 7}", netPay.ToString("F"));
WriteLine("=======================\n");




|
|
|||
| Previous | Copyright © 2010-2026, FunctionX | Wednesday 15 October 2025, 22:32 | Next |
|
|
|||