Free language processing service and NLP C# code
Current topic Practical tutorials for Visual Studio

Quite often we need to debug an application either to capture the errors of our own code or to closely follow the logic of the code written by others. Visual Studio offers very powerful and easy-to-use debugging tools. This tutorial demonstrates the three debugging methods: Step Into, Stem Over, and Step Out.

Create a new Windows Forms application called Debugging. Drag and drop a Button and a RichTextBox onto the form designer. Press Ctrl + F5 to produce something like Figure 1.

Application with a Button and a RichTextBox
Fig. 1

Next create a text file with the content of I am who I am. Save it as TextFile1.txt in the directory of ..\Debugging\Debug\Bin\, Figures 2, 3, and 4 illustrating the steps.

Create new file
Fig. 2 Create new file.

Select the Text file template.
Fig. 3 Select the Text file template.

Save as TextFile1.txt.
Fig. 4 After typing 'I am who I am', save the file as 'TextFile1.txt'.

Double click the button to get to the code placeholder. Add the code, including the helper method AddHypen(string text) shown in Figure 5. We want the running of the application to pause after the button is clicked. For that purpose, we need to set a break point to the left of the line number of private void btnLoadFile_Click(object sender, EventArgs e), which is marked by a red ball. Set a break point for the line of richTextBox1.AppendText(newText); as well. See Figure 5 for the final result so far.

As a side note, if your Visual Studio has no line numbers displayed, you can make them appear by Tools > Options > Text Editor > All Languages > Check Line numbers in the right panel.

Add code and set two break points
Fig. 5 Add code and set two break points.

Press F5 and click the Load File button to bring up the debugging menu. In this tutorial, we'll talk about three debugging buttons representing Step Into, Step Over, and Step Out. See Figure 6 for their icons and locations.

Debugging menu
Fig. 6 Debugging menu

Step Into Step Into

Step Into (F11) executes code one statement at a time. If the statement is invoking another procedure, the next statement displayed is the first statement of that invoked procedure. During the process of debugging, the values of the variables are displayed in the Locals Window (see Figure 7).

Locals Window
Fig. 7 Locals Window

To see how this works in action, click the Step Into button all way down. When it comes to the line string newText = AddHypens(text);, clicking Step Into button leads you to the first statement of that method.

Step Over Step Over

Step Over (F10) is similar to Step Into. You notice the difference only when the current statement contains a call to another procedure. Step Over executes the invoked procedure as a unit. This means that instead of displaying each line of code of that procedure, it goes to the next statement of the current procedure. You use the Step Over function when you are interested in not the logic but the result of the invoked procedure.

To see this in action, when it comes to the line string newText = AddHypens(text);, click the Step Over button. You will be led to the next statement of the current method, namely, richTextBox1.AppendText(newText); with the value of the variable newText computed and displayed in the Locals Window.

Step Out Step Out

Step Out (Shift + F11) executes the remaining lines of code to the end of the program or the next break point. You use this function when you want to skip some lengthy code, such as a big loop.

To see this in action, click the Load File button, then click the Step Out button. You will be directly taken to the line of richTextBox1.AppendText(newText); with the values of the involved variables displayed in the Locals Window.

In this tutorial, we learned how to debug code by using Step Into, Step Over, and Step Out.

This wraps up my tutorials for Visual Studio. The next page will lead you to two external links to more tutorials for Visual Studio and programming in C#.

Skip Navigation LinksHome > Visual Studio Tutorials > How to Debug