17.3. Studio: If It Ain’t Broke, Add a Breakpoint!

17.3.1. Introduction

The purpose of this studio is to talk about your current debugging strategies and how to make the most of the debugger tools discussed in this chapter.

Before we start practicing with debugging tools, go over with the group one error you encountered with any exercise, studio, or book example using Visual Studio. This could be the result of a typo or a logical error.

  1. What was the error?

  2. How did you solve this error? What have been the strategies and tools you have been using so far to debug your code?

  3. Could one of the debugging tools help you when addressing this error? For example, if you encountered an error where scope was interferring with access to data, could you find the accessibility of the data with a debugging tool?

17.3.2. Part 1: Calculate The Area Of A Circle

As you build your code for today’s studio, explore the colorations, icons, and other intellisense features of Visual Studio. Don’t forget to try the debugger if something isn’t working as you planned, or even if it is working as you planned.

To get started: Create a new console application in Visual Studio for the studio. You can refer back to how to create a new C# project.

Write a program that prompts the user for the radius of a circle. Calculate the area of the user’s circle and print the result.

Tip

Recall that the area of a circle is A = pi * r * r where pi is 3.14 and r is the radius.

Here’s an example of how your program should work:

Enter a radius:
2.5
The area of a circle of radius 2.5 is: 19.625

Some questions to ask yourself:

  1. What data type should the radius be?

  2. What is the best way to get user input into a variable radius of that type?

  3. Do you really need more than 3 decimal places?

Note

Don’t forget the Math class and the built-in PI field.

17.3.2.1. More Calculations

  1. Using the same radius, calculate the circumference (2*pi*r) and diameter of the circle (2*r).

  2. Output the results.

17.3.2.2. Road Trip!

  1. Ask the user for the miles per gallon of their car.

  2. If the radius that they entered is in miles, output how many gallons of gas they will use to go around this circle.

17.3.3. Part 2: Working With The Debugger

To get started, try the following:

  1. Add circumference to the Watch pane to track the value of that property. Does it change?

  2. Add a few breakpoints inside of Program.cs and make note of where you expect the program to break its execution.

After you look through the code and try out these tasks, take it one step further by answering these questions.

  1. When would you use the Call Stack pane? If you run the app and it is already functioning, what shows up there?

  2. Would a conditional breakpoint make sense to use in the context of this app? Try changing one of the breakpoints you have already added to a conditional breakpoint and run your app!

Once you have gone through the code, open up a piece of code you have been struggling with. In what ways could making use of debugging tools help you figure out what is going on with the code?

17.3.4. Bonus Missions If You Want To Expand Your Code

  1. Think about how we could make this program more modular by breaking out some of the code into a separate class. For example, we could pull out the circle information into a Circle class and leave the user questions and console messages in Program. Take a look at for a refresher on using another class file.

  2. Extend your program further by creating a way to prevent negative numbers from being accepted.

  3. Add additional validation to your program. If the user enters a non-numeric character or a empty string? Print an error message and quit. You’ll need to peek ahead to learn about conditional syntax in C#.