0

members

Easy CBASIC Lesson 9 Compiling and debugging in Visual Studio

of Acoptex.com in Csharp

CBASIC Learning C# basics

Tags: C#, Csharp, C# fundamentals, C# basics

These lessons are designed to keep you engaged with the process of learning C# basics. Our main focus will be on learning C# basics. 

Lesson 9 C# Compiling and debugging in Visual Studio

In this lesson we will show you how to compile and debug in Visual Studio

1. Compiling

The compiling process in Visual Studio includes several steps:

  • Syntax error check;
  • A check for other errors, like missing libraries;
  • Converting the C# code into an executable file (file with .exe extension).

To compile a file in Visual Studio press F6 key or Shift+Ctrl+B keys. Usually, errors are underlined in red, to attract the programmer’s attention, while you are still writing or when compiling, at the latest. They are listed in the Error List window if it is visible (if it is not, we can select it from the View menu of Visual Studio).

If your project has at least one error, it will be marked with a small red x in the Error List window. Short info about the problem is displayed for each error – filename, line number and project name. If you double click any of the errors in the Error List, Visual Studio will automatically bring you to the file and line of code where the error has occurred.

2. Debugging

When your program contains errors (bugs), you must find and remove them, i.e. you need to debug the program. The debugging process includes:

  • Noticing the errors (bugs);
  • Finding the code causing the problems;
  • Fixing the code so that the program works correctly;
  • Testing to make sure the program works as expected after the changes are made.

The process can be repeated several times until the program starts working correctly.

Visual Studio can help by allowing us to check step by step whether everything is working as planned. To stop the execution of the program at designated positions we can place breakpoints. The breakpoint is associated with a line of the program. The program stops its execution on the lines with breakpoints, allowing for the rest of the code to be executed step by step. On each step you can check and even change the values of the current variables.

Debugging is a sort of step by step slow motion execution of the program. It gives you the opportunity to easily understand the details of the code and see where exactly and why the errors have occurred. Let’s create an intentional error in our program, to illustrate how to use breakpoints - add a line to the program, which will create an exception during the execution:

throw new System.NotImplementedException(

"Intended exception.");

When you start the program again with Ctrl+F5 it will be an error and it will be printed on the console:

Let’s see how breakpoints helps you to find the problem. Move the cursor to the line with the opening bracket of the Main() method and press F9 key (by doing so you place a breakpoint on that line). A red dot appears, indicating that the program will stop there if it is executed in debug mode:

Now you must start the program in debug mode. Select Debug -> Start Debugging or press F5 key. The program will start and immediately stop at the first breakpoint it encounters. The line will be colored in yellow and the program can be executed step by step. With the F10 key you move to the next line. When you are on a given line and it is colored in yellow, the code on that line is not executed yet. It executes once we have passed that line. In this case you have not received the error yet despite the fact that we are on the line you added and should cause it:

Press F10 key one more time to execute the current line. This time Visual Studio displays a window specifying the line, where the error occurred as well as some additional details about it:

Once you know where exactly the problem in the program is, it's easy to correct it. First, you need to stop the execution of the program before it is finished. Select Debug –> Stop Debugging or press Shift+F5. After that you delete the problem line and start the program in normal mode (without debugging) by pressing Ctrl+F5.

Next -> CBASIC Lesson 10 Decompiling Code



Other projects of Acoptex.com

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 10-04-2019
Viewed: 259 times