Fundamentals of 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 LearningPractical Learning: Introducing Errors

  1. Start Microsoft Visual Studio
  2. Create a new Console App named WattsALoan2
  3. In the Solution Explorer, right-click Program.cs -> Rename
  4. Type LoanEvaluation (to get LoanEvaluation.cs) and press Enter
  5. Change the document as follows:
    using static System.Console;
    
    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:        ");
    double principal = double.Parse(ReadLine()!);
    
    Write("Enter the interest rate:    ");
    double interestRate = double.Parse(ReadLine()!);
    
    Write("Enter the number of months: ");
    int periods = int.Parse(ReadLine()!) / 12;
    
    double iRate   = interestRate / 100;
    double interestAmount = principal * iRate * periods;
    double futureValue    = principal + interestAmount;
    
    WriteLine("===============================================");
    WriteLine("Loan Summary");
    WriteLine("=---------------------------------------------=");
    WriteLine("Principal:                  {0:F}", principal);
    WriteLine("Interest Rate:              {0:P}", iRate);
    WriteLine("Period For:                 {0} months", periods);
    WriteLine("Interest Amount:            {0:F}", interestAmount);
    WriteLine("Future Value:               {0:F}", futureValue);
    WriteLine("==============================================");
    
  6. To start debugging, on the main menu, click Debug -> Start Debugging
  7. Enter the values requested and press Enter after each:
    This application allows you to evaluate a loan.
    To proceed, enter the following values.
    =---------------------------------------------=
    Enter the principal:        3725.85
    Enter the interest rate:    8.765
    Enter the number of months: 36
    ===============================================
    Loan Summary
    =---------------------------------------------=
    Principal:                  3725.85
    Interest Rate:              876.50%
    Period For:                 36 months
    Interest Amount:            979.71
    Future Value:               4705.56
    ==============================================
    
    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 . . .
  8. In the DOS window, press A to close it and return to your programming environment

Creating a Breakpoint

A breakpoint is a line (or section) of code where you want the execution to pause until you, as the developer or applicatin tester, decide to continue. 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:

Breakpoint

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:

Breakpoint

Practical LearningPractical Learning: Creating and Using a Breakpoint

  1. In the Code Editor, click the margin on the left side of "double futureValue = principal + interestAmount"

    Breakpoint

  2. On the main menu, click Debug -> Start Debugging.
    Notice that the DOS window displays

    Debugging - Running to a Breakpoint

    If necessary, click the Locals tab to show the contents of its window
  3. In the DOS window, enter the value of the principal as 6255.75 and press Enter
  4. For the interest rate, type 9.725 and press Enter
  5. For the number of months, type 48 and press Enter
    The focus moves back to Microsoft Visual Studio:

    Debugging - Running to a Breakpoint

    Notice the values in the Locals window.
    Position the mouse on each variable in the Code Editor and observe the value
  6. To continue debugging, on the main menu of Microsoft Visual Studio, click Debug and click Continue:
    This application allows you to evaluate a loan.
    To proceed, enter the following values.
    =---------------------------------------------=
    Enter the principal:        6255.75
    Enter the interest rate:    9.725
    Enter the number of months: 48
    ===============================================
    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 . . .

Creating Breakpoints

Usually, you want to test various sections of your code for different reasons or goals. As a result, you can create as many breakpoints as you want. When debugging, the debugger would stop at each breakpoint. You can then continue when you have examined code at the selected breakpoint.

Practical LearningPractical Learning: Creating and Using Breakpoints

  1. In the Code Editor, click anywhere on the double iRate = interestRate / 100; line to give it focus
  2. On the main menu of Microsoft Visual Studio, click Debug and click Toggle Breakpoint
  3. In the Code Editor, right-click anywhere on the WriteLine("Interest Amount: {0:F}", interestAmount); line -> Breakpoint -> Insert Breakpoint:

    Debugging - Creating Breakpoints

  4. To start debugging, press F5
  5. When requested, enter the value of the principal as 18225.45 and press Enter
  6. For the interest rate, type 6.815 and press Enter
  7. For the number of months, type 60 and press Enter
  8. The focus moves to Microsoft Visual Studio

    Debugging - Running to a Breakpoint

    Notice the values in the Locals window.
    Position the mouse on each variable in the Code Editor and observe the value
  9. To continue debugging, press F5.

    Debugging - Running to a Breakpoint

    Notice the values in the Locals window.
    Position the mouse on each variable in the Code Editor and observe the value
  10. To continue debugging, press F5:

    Debugging - Running to a Breakpoint

    This application allows you to evaluate a loan.
    To proceed, enter the following values.
    =---------------------------------------------=
    Enter the principal:        18225.45
    Enter the interest rate:    6.815
    Enter the number of months: 60
    ===============================================
    Loan Summary
    =---------------------------------------------=
    Principal:                  18225.45
    Interest Rate:              6.82%
    Period For:                 5 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 . . .
  11. Return to your programming environment

Stepping Into 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 LearningPractical Learning: Stepping to Breakpoints

  1. Make sure the previous breakpoints are still set.
    To start debugging, on the main menu, click Debug -> Step Into.
    Make sure you can see the Code Editor, the Locals window, and the DOS window

    Debugging - Breakpoint - Stepping Into ...

  2. Click the title bar of Microsoft Visual Studio to give it focus.
    To continue debugging, on the Debug toolbar, click the Step Into button Step Into continuously (8 times) until the DOS window receives focus
  3. When the value of the principal is requested, type 6535.85 and press Enter
    The focus moves to Microsoft Visual Studio

    Debugging - Breakpoint - Stepping Into ...

  4. To continue debugging, on the Debug toolbar, click the Step Into button Step Into. Notice the value on the right side of the closing curly bracket on the GetPrincipal() function:

    Debugging - Breakpoint - Stepping Into ...

  5. Press F11 to continue.
    The focus moves to the "double principal = GetPrincipal();" line

    Debugging - Breakpoint - Stepping Into ...

  6. Press F11 to continue.
    The focus moves to the "double interestRate = GetInterestRate() / 100;" line

    Debugging - Breakpoint - Stepping Into ...

  7. Press F11 to continue.
    The focus moves to the GetInterestRate() function
  8. Press F11 continually (4 times) until the focus moves to the DOS window
  9. For the interest rate, type 7.625 and press Enter.
    The focus moves to the last line of the GetInterestRate() function
  10. Press F11 to continue. Notice the value on the right side of the closing curly bracket on the GetInterestRate() function:

    Debugging - Breakpoint - Stepping Into ...

  11. Press F11 to continue:

    Debugging - Breakpoint - Stepping Into ...

  12. Press F11 continuously (four times) until the focus moves to the GetPeriods() function
  13. Press F11 to move the focus to the DOS window
  14. For the number of months, type 48 and press Enter

    Debugging - Breakpoint - Stepping Into ...

    The focus moves to Microsoft Visual Studio
  15. Press F11 to continue. Notice the value on the right side of the closing curly bracket of the GetPeriods() function:

    Debugging - Breakpoint - Stepping Into ...

  16. Press F11 to continue:

    Debugging - Breakpoint - Stepping Into ...

  17. Press F11 to continue:

    Debugging - Breakpoint - Stepping Into ...

  18. Press F11 continually. Watch how focus moves to the lines in the Code Editor. Also watch the values in the Locals window

    Debugging - Breakpoint - Stepping Into ...

    This application allows you to evaluate a loan.
    To proceed, enter the following values.
    =---------------------------------------------=
    Enter the principal:        6535.85
    Enter the interest rate:    7.625
    Enter the number of months: 48
    ===============================================
    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 . . .
  19. Return to your programming environment

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:

Breakpoints Window

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:

Breakpoints Window

Removing a Breakpoint

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

Deleting all Breakpoints

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 know 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. When a breakpoint is disabled:

To disable a breakpoint:

Breakpoints and Functions

Breakpoints

We know that, in a source file, we can create functions. We can then involve breakpoints in the functions. We can create a breakpoint inside or outside a function.

Practical LearningPractical Learning: Introducing Function Debugging

  1. Create a new Console App named ObliqueTriangles3
  2. Change the document as follows:
    using static System.Console;
    
    void CalculateAAS()
    {
        double angle1 = 0.00;
        double angle2 = 0.00;
        double side1  = 0.00;
    
        WriteLine("AAS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  Known Values: 2 sides and the 1 angle between them");
        WriteLine("Sought Values: 2 angles and the 1 sides between them");
        WriteLine("----------------------------------------------------------------------");
    
        try
        {
            Write("AAS Angle 1: ");
            angle1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the lower left angle of the AAS shape.");
        }
    
        try
        {
            Write("AAS Angle 2: ");
            angle2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the lower right angle of the AAS shape.");
        }
    
        try
        {
            Write("AAS Side 1:  ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the right side of the AAS shape.");
        }
    
        // Here, we use the law of sines
        double angle3 = 180 - (angle1 + angle2);
        double side2 = side1 * Math.Sin(angle2 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
        double side3 = side1 * Math.Sin(angle3 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("AAS Angle 3:  " + angle3.ToString());
        WriteLine("AAS Side  2:  " + side2.ToString());
        WriteLine("AAS Side  3:  " + side3.ToString());
    }
    
    void CalculateASA()
    {
        double angle1 = 0.00, angle2 = 0.00, side1 = 0.00;
    
        WriteLine("ASA Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  2 angles and the 1 side joining them");
        WriteLine("Sought Values: 2 sides and the 1 angle between them");
        WriteLine("----------------------------------------------------------------------");
    
        try
        {
            Write("ASA Angle 1: ");
            angle1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known angles of the ASA shape.");
        }
    
        try
        {
            Write("ASA Side 1:  ");
            side1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the side that is between the angles whose values are about the ASA shape.");
        }
    
        try
        {
            Write("AAS Angle 2: ");
            angle2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the other known angle of the ASA oblique triangle.");
        }
    
        // Here, we use the law of sines
        double angle3 = 180 - (angle1 + angle2);
        double side2 = side1 * Math.Sin(angle2 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
        double side3 = side1 * Math.Sin(angle3 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("ASA Angle 3: {0}", angle3);
        WriteLine("ASA Side  2:  {0}", side3);
        WriteLine("ASA Side  3:  {0}", side2);
    }
    
    void CalculateSAS()
    {
        WriteLine("SAS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  2 sides and 1 angle");
        WriteLine("Sought Values: 1 unknown side and 2 unknown angles");
        WriteLine("----------------------------------------------------------------------");
    
        double side1 = 0.00, angle1 = 0.00, side2 = 0.00;
    
        WriteLine("Type the known values");
    
        try
        {
            Write("SAS Side  1: ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known sides of the SAS shape.");
        }
    
        try
        {
            Write("SAS Angle 1: ");
            angle1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the known angle of the SAS shape.");
        }
    
        try
        {
            Write("SAS Side  2: ");
            side2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the other known side of the SAS shape.");
        }
    
        // Here, we use the law of cosines
        double side3 = Math.Sqrt((side1 * side1) +
                                 (side2 * side2) -
                                 (2 * side1 * side2 * Math.Cos(angle1 * Math.PI / 180)));
        double angle2 = Math.Acos(((side3 * side3) +
                                   (side2 * side2) -
                                   (side1 * side1)) /
                                   (2 * side3 * side2)) * 180 / Math.PI;
        double angle3 = 180 - (angle1 + angle2);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("SAS Side  3: {0}", side3.ToString());
        WriteLine("SAS Angle 2: {0}", angle2.ToString());
        WriteLine("SAS Angle 3: {0}", angle3.ToString());
    }
    
    void CalculateSSS()
    {
        WriteLine("SSS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  All 3 sides");
        WriteLine("Sought Values: All 3 angles");
        WriteLine("----------------------------------------------------------------------");
    
        double side1 = 0.00, side2 = 0.00, side3 = 0.00;
    
        WriteLine("Type the known values");
    
        try
        {
            Write("SSS Side 1:  ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known sides of the SSS shape.");
        }
    
        try
        {
            Write("SSS Side 2:  ");
            side2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for another side of the SSS triangle.");
        }
    
        try
        {
            Write("SSS Side 3:  ");
            side3 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the remaining known side of the SSS oblique triangle.");
        }
    
        // Here, we use the law of cosines
        double angle1 = Math.Acos(((side3 * side3) +
                                   (side2 * side2) -
                                   (side1 * side1)) /
                                   (2 * side3 * side2)) * 180 / Math.PI;
        double angle2 = Math.Acos(((side3 * side3) +
                                   (side1 * side1) -
                                   (side2 * side2)) /
                                   (2 * side3 * side1)) * 180 / Math.PI;
        double angle3 = 180 - (angle1 + angle2);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine($"SSS Angle 1: {angle1}");
        WriteLine($"SSS Angle 2: {angle2}");
        WriteLine($"SSS Angle 3: {angle3}");
    }
    
    WriteLine("======================================================================");
    WriteLine("This application allows you to make calculations on oblique triangles");
    WriteLine("======================================================================");
    
    Title = "Oblique Triangles";
    
    WriteLine("Available shape options:");
    WriteLine("1 - AAS (Known: Angle - Angle - Side)");
    WriteLine("2 - ASA (Known: Angle - Side - Angle)");
    WriteLine("3 - SAS (Known: Side - Angle - Side)");
    WriteLine("4 - SSS (Known: Side - Side - Side)");
    WriteLine("Q - Quit");
    Write("What type of triangle do you want to process? ");
    string selection = ReadLine()!;
    
    switch (selection)
    {
        case null:
            break;
        case "4":
            CalculateSSS();
            break;
        case "3":
            CalculateSAS();
            break;
        default:
            break;
        case "1":
            CalculateAAS();
            break;
        case "2":
            CalculateASA();
            break;
    }
    
    WriteLine("======================================================================");

Calling a Function

One way to monitor the return value of a function is to put a breakpoint where a function is called.

Practical LearningPractical Learning: Introducing Breakpoints with Functions

  1. In the Code Editor, click the margin on the left side of "double periods = GetPeriods();"

    Breakpoint

  2. On the main menu, click Debug -> Start Debugging.
    Notice that the DOS window displays

    Debugging - Running to a Breakpoint

  3. When asked, enter the value of the principal as 6255.75 and press Enter
  4. For the interest rate, type 9.725 and press Enter
  5. The focus moves back to Microsoft Visual Studio:

    Debugging - Running to a Breakpoint

  6. To continue debugging, on the main menu of Microsoft Visual Studio, click Continue
  7. For the number of months, type 48 and press Enter:
    This application allows you to evaluate a loan.
    To proceed, enter the following values.
    =---------------------------------------------=
    Enter the principal:        6255.75
    Enter the interest rate:    9.725
    Enter the number of months: 48
    ===============================================
    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 . . .
    The focus moves back to Microsoft Visual Studio
  8. To remove the breakpoint, on the main menu, click Debug -> Toggle Breakpoint

Creating Breakpoints

Usually, you want to test various sections of your code for different reasons or goals. As a result, you can create as many breakpoints as you want. When debugging, the debugger would stop at each breakpoint. You can then continue when you have examined code at the selected breakpoint.

Practical LearningPractical Learning: Creating and Using Breakpoints

  1. In the Code Editor, click anywhere on the double interestRate = GetInterestRate(); line to give it focus
  2. On the main menu of Microsoft Visual Studio, click Debug and click Toggle Breakpoint
  3. In the Code Editor, right-click anywhere on the interestAmount = principal * interestRate * periods; line -> Breakpoint -> Insert Breakpoint:

    Debugging - Creating Breakpoints

  4. To start debugging, press F5
  5. When requested, enter the value of the principal as 18225.45
  6. And press Enter

    Debugging - Running to a Breakpoint

  7. To continue debugging, press F5.
    The focus moves to the DOS window
  8. For the interest rate, type 6.815 and press Enter
  9. For the number of months, type 60 and press Enter

    Debugging - Running to a Breakpoint

    The focus moves to Microsoft Visual Studio
  10. To continue debugging, press F5:
    Enter the interest rate:    6.815
    Enter the number of months: 60
    ===============================================
    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 . . .
  11. Return to your programming environment

Stepping Into 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 LearningPractical Learning: Stepping to Breakpoints

  1. Make sure the previous two breakpoints are still selected.
    To start debugging, on the main menu, click Debug -> Step Into.
    Make sure you can see the Code Editor, the Locals window, and the DOS window

    Debugging - Breakpoint - Stepping Into ...

  2. Click the title bar of Microsoft Visual Studio to give it focus.
    To continue debugging, on the Debug toolbar, click the Step Into button Step Into continuously (6 times) until the DOS window receives focus
  3. When the value of the principal is requested, type 6535.85 and press Enter
    The focus moves to Microsoft Visual Studio

    Debugging - Breakpoint - Stepping Into ...

    Notice the values in the Loc
  4. To continue debugging, on the Debug toolbar, click the Step Into button Step Into. Notice the value on the right side of the closing curly bracket on the GetPrincipal() function:

    Debugging - Breakpoint - Stepping Into ...

  5. Press F11 to continue.
    The focus moves to the "double principal = GetPrincipal();" line

    Debugging - Breakpoint - Stepping Into ...

  6. Press F11 to continue.
    The focus moves to the "double interestRate = GetInterestRate() / 100;" line

    Debugging - Breakpoint - Stepping Into ...

  7. Press F11 to continue.
    The focus moves to the GetInterestRate() function
  8. Press F11 continually (4 times) until the focus moves to the DOS window
  9. For the interest rate, type 7.625 and press Enter.
    The focus moves to the last line of the GetInterestRate() function
  10. Press F11 to continue. Notice the value on the right side of the closing curly bracket on the GetInterestRate() function:

    Debugging - Breakpoint - Stepping Into ...

  11. Press F11 to continue:

    Debugging - Breakpoint - Stepping Into ...

  12. Press F11 continuously (four times) until the focus moves to the GetPeriods() function
  13. Press F11 to move the focus to the DOS window
  14. For the number of months, type 48 and press Enter

    Debugging - Breakpoint - Stepping Into ...

    The focus moves to Microsoft Visual Studio
  15. Press F11 to continue. Notice the value on the right side of the closing curly bracket of the GetPeriods() function:

    Debugging - Breakpoint - Stepping Into ...

  16. Press F11 to continue:

    Debugging - Breakpoint - Stepping Into ...

  17. Press F11 to continue:

    Debugging - Breakpoint - Stepping Into ...

  18. Press F11 continually. Watch how focus moves to the lines in the Code Editor. Also watch the values in the Locals window

    Debugging - Breakpoint - Stepping Into ...

    This application allows you to evaluate a loan.
    To proceed, enter the following values.
    =---------------------------------------------=
    Enter the principal:        6535.85
    Enter the interest rate:    7.625
    Enter the number of months: 48
    ===============================================
    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 . . .
  19. Return to your programming environment

Breaking a Point in a Function

One way to monitor the behavior of a function is to put a breakpoint in the body of a function.

Calling a Function

One way to monitor the return value of a function is to put a breakpoint where a function is called.

Breakpoints with Objects and Files

Introduction

In the previous sections, we created all our breakpoints in one source file. 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.

Breakpoints and Classes

Practical LearningPractical Learning: Debugging With a Class

  1. Start Microsoft Visual Studio
  2. Create a new C# Console App named WattsALoan2
  3. To create a folder, in the Solution Explorer, right-click WattsALoan02 -> Add -> New Folder
  4. Type Models and press Enter
  5. To create a new class, in the Solution Explorer, right-click Models -> Add -> Class...
  6. Change the Name to Customer
  7. Click Add
  8. Change the class as follows:
    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;
            }
        }
    }
  9. To create another class, in the Solution Explorer, right-click Models -> Add -> Class...
  10. Type Employee
  11. Press Enter
  12. Change the class as follows:
    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;
            }
        }
    }
  13. To add another class to the project, in the Solution Explorer, right-click Models -> Add -> Class...
  14. Change the Name to LoanInformation
  15. Click Add
  16. Change the class as follows:
    namespace WattsALoan02.Models
    {
        internal class LoanInformation
        {
            public double principal;
            public double interestRate;
            public double period;
            public double interestAmount;
            public double futureValue;
        }
    }
  17. In the Solution Explorer, right-click Programa.cs -> Rename
  18. Type LoanEvaluation (to get LoanEvaluation.cs) and press Enter
  19. Change the document as follows:
    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