Share via


Tutorial: Learn to debug Visual Basic code using Visual Studio

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're running your application with the debugger attached. When you do this task, 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 exercise is the first time that you've tried to debug code, you might 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# doesn't support Edit-and-continue. F# and JavaScript don't 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 2019 installed and the .NET Core cross-platform development workload.

Create a project

First, you create a .NET Core console application project. The project type comes with all the template files you need, before you've even added anything!

  1. Open Visual Studio. If the start window isn't open, select File > Start Window.

  2. On the start window, select Create a new project.

Visual Studio opens your new project.

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

Restart your app quickly

Inspect variables with data tips

Inspect variables with the Autos and Locals windows

Set a watch

Examine the call stack

Change the execution flow

  1. Press F11 twice to run the Console.WriteLine method.

  2. With the debugger paused in the SendMessage method call, use the mouse to grab the yellow arrow or execution pointer (in the left margin), and drag the pointer up one line to the Console.WriteLine statement.

  3. Press F11.

    The debugger reruns the Console.WriteLine method (you see this action 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 might see other warnings, too. Moving the pointer cannot revert your application to an earlier app state.

  4. 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 might want to get a high-level look at debugger features along with links to more information.