Tutorial: Learn to debug Visual Basic code using Visual Studio
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
This article introduces the features of the Visual Studio debugger in a step-by-step walkthrough. If you want a higher-level view of the debugger features, see First look at the debugger. When you debug your app, it usually means that you are running your application with the debugger attached. When you do this, the debugger provides many ways to see what your code is doing while it runs. You can step through your code and look at the values stored in variables, you can set watches on variables to see when values change, you can examine the execution path of your code, see whether a branch of code is running, and so on. If this is the first time that you've tried to debug code, you may want to read Debugging for absolute beginners before going through this article.
Although the demo app is Visual Basic, most of the features are applicable to C#, C++, F#, Python, JavaScript, and other languages supported by Visual Studio (F# does not support Edit-and-continue. F# and JavaScript do not support the Autos window). The screenshots are in Visual Basic.
In this tutorial, you will:
- Start the debugger and hit breakpoints.
- Learn commands to step through code in the debugger
- Inspect variables in data tips and debugger windows
- Examine the call stack
Prerequisites
You must have Visual Studio 2017 installed and the .NET Core cross-platform development workload.
If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
Create a project
First, you'll create a .NET Core console application project. The project type comes with all the template files you'll need, before you've even added anything!
Open Visual Studio 2017.
From the top menu bar, choose File > New > Project.
In the New Project dialog box in the left pane, expand Visual Basic, and then choose .NET Core. In the middle pane, choose Console App (.NET Core). Then name the project get-started-debugging.
If you don't see the Console App (.NET Core) project template, choose the Open Visual Studio Installer link in the left pane of the New Project dialog box.
The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.
Create the application
In Program.vb, replace all of the default code with the following code instead:
Imports System Class ArrayExample Public Shared Sub Main() Dim letters As Char() = {"f"c, "r"c, "e"c, "d"c, " "c, "s"c, "m"c, "i"c, "t"c, "h"c} Dim name As String = "" Dim a As Integer() = New Integer(9) {} For i As Integer = 0 To letters.Length - 1 name += letters(i) a(i) = i + 1 SendMessage(name, a(i)) Next Console.ReadKey() End Sub Private Shared Sub SendMessage(ByVal name As String, ByVal msg As Integer) Console.WriteLine("Hello, " & name & "! Count to " & msg) End Sub End Class
Start the debugger!
Set a breakpoint and start the debugger
Navigate code in the debugger using step commands
Navigate code using Run to Click
Restart your app quickly
Inspect variables with data tips
Inspect variables with the Autos and Locals windows
Set a watch
In the main code editor window, right-click the
name
variable and choose Add Watch.The Watch window opens at the bottom of the code editor. You can use a Watch window to specify a variable (or an expression) that you want to keep an eye on.
Now, you have a watch set on the
name
variable, and you can see its value change as you move through the debugger. Unlike the other variable windows, the Watch window always shows the variables that you are watching (they're grayed out when out of scope).
Examine the call stack
Change the execution flow
Press F11 twice to run the
Console.WriteLine
method.With the debugger paused in the
SendMessage
method call, use the mouse to grab the yellow arrow (the execution pointer) on the left and move it up one line, back toConsole.WriteLine
.Press F11.
The debugger reruns the
Console.WriteLine
method (you see this in the console window output).By changing the execution flow, you can do things like test different code execution paths or rerun code without restarting the debugger.
Warning
Often you need to be careful with this feature, and you see a warning in the tooltip. You may see other warnings, too. Moving the pointer cannot revert your application to an earlier app state.
Press F5 to continue running the app.
Congratulations on completing this tutorial!
Next steps
In this tutorial, you've learned how to start the debugger, step through code, and inspect variables. You may want to get a high-level look at debugger features along with links to more information.