Debugging some Values
Debugging some Values
Value Debugging
Introduction
In the previous lessons, we were introduced to debugging and some of the tools that Microsoft Visual Studio provides. The intent was to be introduced to some means of identifying the variables and monitoring their values.
Practical Learning: Introducing Errors
using static System.Console; int employeeId = default; long number = 0L; string? firstName = null; string? lastName = null; double hourlySalary = 0.00; double timeWorked = 0; double netPay = hourlySalary * timeWorked; WriteLine("Payroll Evaluation"); WriteLine("==========================="); WriteLine("Employee Record"); WriteLine("---------------------------"); WriteLine("Employee Id:\t{0}", employeeId); WriteLine("Employee #:\t{0}", number); WriteLine("First Name:\t{0}", firstName); WriteLine("Last Name:\t{0}", lastName); WriteLine("Hourly Salary:\t{0}", hourlySalary); WriteLine("Time Worked:\t{0}", timeWorked); WriteLine("Net Pay:\t{0}", netPay); WriteLine("===========================");
Observing Variable Changes
One of the most valuable techniques that Microsoft Visual Studio offers is to observe the value of a variable directly in the Code Editor as soon as that value is changed. You have many options. The primary option is to position the mouse at any time on any part that the variable is used and observe its value.
Practical Learning: Observing Variable Changes
using static System.Console;
int employeeId = default;
long employeeNumber = 0L;
string? firstName = null;
string? lastName = null;
double hourlySalary = 0.00;
double timeWorked = 0;
double netPay = hourlySalary * timeWorked;
WriteLine("Payroll Evaluation");
WriteLine("===========================");
WriteLine("Employee Record");
WriteLine("---------------------------");
WriteLine("Employee Id:\t{0}", employeeId);
WriteLine("Employee #:\t{0}", employeeNumber);
WriteLine("First Name:\t{0}", firstName);
WriteLine("Last Name:\t{0}", lastName);
WriteLine("Hourly Salary:\t{0}", hourlySalary);
WriteLine("Time Worked:\t{0}", timeWorked);
WriteLine("Net Pay:\t{0}", netPay);
WriteLine("===========================");
employeeId = 1;
employeeNumber = 597_485;
firstName = "Lynda";
lastName = "Elroy";
hourlySalary = 25.73;
timeWorked = 36.50;
netPay = hourlySalary * timeWorked;
WriteLine("Payroll Evaluation");
WriteLine("===========================");
WriteLine("Employee Record");
WriteLine("---------------------------");
WriteLine("Employee Id:\t{0}", employeeId);
WriteLine("Employee #:\t{0}", employeeNumber);
WriteLine("First Name:\t{0}", firstName);
WriteLine("Last Name:\t{0}", lastName);
WriteLine("Hourly Salary:\t{0}", hourlySalary);
WriteLine("Time Worked:\t{0}", timeWorked);
WriteLine("Net Pay:\t{0}", netPay);
WriteLine("===========================");");Payroll Evaluation =========================== Employee Record --------------------------- Employee Id: 0 Employee #: 0 First Name: Last Name: Hourly Salary: 0 Time Worked: 0 Net Pay: 0 =========================== Payroll Evaluation =========================== Employee Record --------------------------- Employee Id: 1 Employee #: 597485 First Name: Lynda Last Name: Elroy Hourly Salary: 25.73 Time Worked: 36.5 Net Pay: 939.145 =========================== Press any key to close this window . . .
using static System.Console; int employeeId = 2; long employeeNumber = 249_358; string? firstName = "Robert"; string? lastName = "Denison"; double hourlySalary = 30.08; double timeWorked = 38.50; double netPay = hourlySalary * timeWorked; WriteLine("Payroll Evaluation"); WriteLine("==========================="); WriteLine("Employee Record"); WriteLine("---------------------------"); WriteLine("Employee Id:\t{0}", employeeId); WriteLine("Employee #:\t{0}", employeeNumber); WriteLine("First Name:\t{0}", firstName); WriteLine("Last Name:\t{0}", lastName); WriteLine("Hourly Salary:\t{0}", hourlySalary); WriteLine("Time Worked:\t{0}", timeWorked); WriteLine("Net Pay:\t{0}", netPay); WriteLine("==========================="); employeeId = 3; employeeNumber = 794_837; firstName = "Rachel"; lastName = "Nielsen"; hourlySalary = 22.88; timeWorked = 42.50; WriteLine("Payroll Evaluation"); WriteLine("==========================="); WriteLine("Employee Record"); WriteLine("---------------------------"); WriteLine("Employee Id:\t{0}", employeeId); WriteLine("Employee #:\t{0}", employeeNumber); WriteLine("First Name:\t{0}", firstName); WriteLine("Last Name:\t{0}", lastName); WriteLine("Hourly Salary:\t{0}", hourlySalary); WriteLine("Time Worked:\t{0}", timeWorked); WriteLine("Net Pay:\t{0}", netPay); WriteLine("===========================");
Running to the Cursor
Imagine that, when your code is executing, there is some behavior that is a concern to you about a certain line of code. Instead of debugging the whole document, you can ask the debugger to jump straight to that particular line. To assist you with this operation, Microsoft Visual Studio provides the Run To Cursor feature. It is available from the context-sensitive menu.
Practical Learning: Running to the Cursor
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"); WriteLine("To proceed, enter the following values"); WriteLine("=---------------------------------------------="); Write("Enter the principal: "); principal = double.Parse(ReadLine()!); Write("Enter the interest rate: "); interestRate = double.Parse(ReadLine()!) / 100; Write("Enter the number of months: "); period = double.Parse(ReadLine()!) / 12; interestAmount = principal * interestRate * period; futureValue = principal + interestAmount; 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("==============================================");
Visualizing a Variable
Introduction
In previous sections and lessons, we saw various ways to monitor the variables and their values. Other features of Microsoft Visual Studio are available. For example, after starting a debugging session, position your mouse on a variable. From there, other options to debug would be offered to you.
Debugging and Data Entry
So far, in our introduction to debugging, we provided the values to monitor on variables. In most of your programs, the users will provide the values your program needs to use. As the users are doing that, you may want to observe the changes, be able to identify the problems and then fix them.
Practical Learning: Debugging while Entering Data
This application allows you to evaluate a loan To proceed, enter the following values =---------------------------------------------= Enter the principal: 6500 Enter the interest rate: 12.625 Enter the number of months: 48 =============================================== Loan Summary =---------------------------------------------= Principal: 6500.00 Interest Rate: 12.63% Period For: 48 months Interest Amount: 3282.50 Future Value: 9782.50 ============================================== Press any key to close this window . . .
using static System.Console;
Title = "Payroll Evaluation";
int employeeId = 4;
long number = 938_074;
string? firstName = "Anthony";
string? lastName = "Jenkins";
double hourlySalary = 0.00;
double timeWorked = 0;
double netPay = hourlySalary * timeWorked;
WriteLine("Payroll Evaluation");
WriteLine("===========================");
WriteLine("Employee Record");
WriteLine("---------------------------");
WriteLine("Employee Id:\t{0}", employeeId);
WriteLine("Employee #:\t{0}", number);
WriteLine("First Name:\t{0}", firstName);
WriteLine("Last Name:\t{0}", lastName);
WriteLine("Hourly Salary:\t{0}", hourlySalary);
WriteLine("Time Worked:\t{0}", timeWorked);
WriteLine("Net Pay:\t{0}", netPay);
WriteLine("===========================");Payroll Evaluation =========================== Employee Record --------------------------- Employee Id: 4 Employee #: 938074 First Name: Anthony Last Name: Jenkins Hourly Salary: 0 Time Worked: 0 Net Pay: 0 =========================== Press any key to close this window . . .
Testing the Values in the Debugging Windows
We have seen that the Locals window is there to assist us in observing the values that are changing in the Code Editor. Another way to use the Locals window is to test the values directly in that window. This means that you can manually change a value of a variable in the Locals window and see what happens. Remember that the Autos window also is used to show the variables and their changing values.
Practical Learning: Testing the Values in the Locals Window
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");
WriteLine("To proceed, enter the following values");
WriteLine("=---------------------------------------------=");
Write("Enter the principal: ");
principal = double.Parse(ReadLine()!);
Write("Enter the interest rate: ");
interestRate = double.Parse(ReadLine()!) / 100;
Write("Enter the number of months: ");
period = double.Parse(ReadLine()!) / 12;
interestAmount = principal * interestRate * period;
futureValue = principal + interestAmount;
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("==============================================");Exceptional Debugging
Introduction
As you should know already, an exception is a type of eror that occurs when your program has started running or executing. It is usually not a syntax or a logic error. As a result, that type of error may not be obvious while you are writing your code in the Code Editor. It is usually an error that would either prevent your program from running or would produce unreliable results.
Debugging is an operation that allows you to identify some exceptions that your applications can throw and add code that would deal with them.
Practical Learning: Testing the Values in the Locals Window
Adding Exceptions
As you may know already, exception handling is the ability to address issues related to types of errors in your application. To do that, you should write appropriate code that deals with known exceptions.
Practical Learning: Testing the Values in the Locals Window
using static System.Console;
double futureValue;
double interestAmount;
double period = default;
double principal = default;
double interestRate = default;
Title = "Watts' A Loan?";
WriteLine("This application allows you to evaluate a loan");
WriteLine("To proceed, enter the following values");
WriteLine("=---------------------------------------------=");
try
{
Write("Enter the principal: ");
principal = double.Parse(ReadLine()!);
}
catch (FormatException exc)
{
WriteLine("The value you typed for the principal is not valid.");
WriteLine("The error produced is: " + exc.Message);
}
try
{
Write("Enter the interest rate: ");
interestRate = double.Parse(ReadLine()!) / 100;
}
catch (FormatException exc)
{
WriteLine("The value you typed for the interest rate is not appropriate.");
WriteLine("The error is: " + exc.Message);
}
try
{
Write("Enter the number of months: ");
period = double.Parse(ReadLine()!) / 12;
}
catch (FormatException exc)
{
WriteLine("The number of months you typed cannot be accepted.\n" +
"The application produced the following error: " + exc.Message);
}
interestAmount = principal * interestRate * period;
futureValue = principal + interestAmount;
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("==============================================");This application allows you to evaluate a loan To proceed, enter the following values =---------------------------------------------= Enter the principal: $22850 The value you typed for the principal is not valid. The error produced is: The input string '$22850' was not in a correct format. Enter the interest rate: 8.225% The value you typed for the interest rate is not appropriate. The error is: The input string '8.225%' was not in a correct format. Enter the number of months: 48 months The number of months you typed cannot be accepted. The application produced the following error: The input string '48 months' was not in a correct format. =============================================== Loan Summary =---------------------------------------------= Principal: 0.00 Interest Rate: 0.00% Period For: 0 months Interest Amount: 0.00 Future Value: 0.00 ============================================== Press any key to close this window . . .
|
|
|||
| Previous | Copyright © 2010-2026, FunctionX | Wednesday 12 November 2025, 14:05 | Next |
|
|
|||