Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (2023)

  • article

Applies to: Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (1)visual studyTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (2)Visual Studio for MacTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (3)visual study code

This article provides a step-by-step introduction to the features of the Visual Studio debugger. For a more advanced look at debugging features, seeFirst look at the debugger. when youdebugging your application, which usually means you're running the program with a debugger attached. By doing this, the debugger provides more ways to see what your code is doing at runtime. You can step through code and see values ​​stored in variables, you can set watches on variables to see when values ​​change, you can inspect the execution path of your code, see if a branch of code is executing, and more . If this is your first attempt at debugging your code, you may want to continueDebugging for absolute beginnersbefore reading this article.

While the demo application is Visual Basic, most features work in C#, C++, F#, Python, JavaScript, and other languages ​​supported by Visual Studio (Edit and Continue are not supported for F#. F# and JavaScript are not supportedit waswindow). Screenshots are written in Visual Basic.

In this seminar you will:

  • Start the debugger and hit a breakpoint.
  • Learn about commands for passing code in the debugger
  • Examine variables in datatips and debug windows
  • Check the call stack

prerequisites

You must have Visual Studio 2019 installed and.NET Core cross-platform developmentworkload.

If you don't have Visual Studio installed, go toVisual Studio Downloadspage to install it for free.

If you need to install a workload but already have Visual Studio, go totool>Get tools and features…, which opens the Visual Studio installer. The Visual Studio installer will start. choose.NET Core cross-platform developmentworkload and selectAdjustment.

If you don't have Visual Studio installed, go toVisual Studio Downloadspage to install it for free.

If you need to install a workload but already have Visual Studio, go totool>Get tools and features…, which opens the Visual Studio installer. The Visual Studio installer will start. choose.NET Computing Developmentworkload and selectAdjustment.

create a project

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

  1. Open image studio.

    If the start window does not open, selectdocument>launch window.

  2. Select in the launch windowcreate a new project.

  3. highercreate a new projectwindow, enter or writecomfortin the search field. Then chooseVisual Basicsfrom the list of languages, then selectWindowsfrom the platform list.

    After applying the language and platform filters, selectconsole applicationTemplates for .NET Core and selectNext.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (4)

    noting

    if you don't seeconsole applicationtemplate from which you can downloadcreate a new projectwindow. insideDidn't find what you were looking for?message, selectInstall more tools and featuresconnected. Then select in the Visual Studio installer.NET Core cross-platform developmentworkload.

  4. indeConfigure your new projectwindow, type or enterstart troubleshootingindeThe name of the thingBox. Then chooseNext.

  5. Select the recommended target framework (.NET Core 3.1) or .NET 5, and then selectcabinet.

    Visual Studio opens your new project.

  1. Open image studio.

    If the start window does not open, selectdocument>launch window.

  2. Select in the launch windowcreate a new project.

  3. highercreate a new projectwindow, enter or writecomfortin the search field. Then chooseVisual Basicsfrom the list of languages, then selectWindowsfrom the platform list.

    After applying the language and platform filters, selectconsole applicationTemplates for .NET Core and selectNext.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (5)

    noting

    if you don't seeconsole applicationtemplate from which you can downloadcreate a new projectwindow. insideDidn't find what you were looking for?message, selectInstall more tools and featuresconnected. Then select in the Visual Studio installer.NET Computing Developmentworkload.

  4. indeConfigure your new projectwindow, type or enterstart troubleshootingindeThe name of the thingBox. Then chooseNext.

  5. higherMore informationwindow, make sure the suggested target box is (.NET 6.0) and selectcabinet.

    Visual Studio opens your new project.

application creation

  1. existsprogram.vband replace all the default code with the following code:

    εισαγωγή SystemClass ArrayExample Public Shared Sub Main() Dim Alphabet As Char() = {"f"c, "r"c, "e"c, "d"c, " "c, "s"c, "m"c , "i"c, "t"c, "h"c} Dim navn As String = "" Dim a As Heltal() = Nyt heltal(9) {} For i Som heltal = 0 Til bogstaver. Længde - 1 navn += bogstav(i) a(i) = i + 1 SendMessage(name, a(i)) Next Console.ReadKey() End Sub Private Shared Sub SendMessage(ByVal name As String, ByValmsg As Integer) Console.WriteLine ("Hello," & name & "!count to" & msg) τέλος δευτερεύουσας κατηγορίας

Start debugging!

  1. accordingF5(Debug > Start Debugging) thestart troubleshootingbuttonTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (6)in the debug toolbar.

    F5Start the app with a debugger attached to the app process, but right now we haven't done anything special to test the code. So the app just loads and you see the console output.

    hell! Count to 1 Hello, Fr! Count to 2 Hey Frey! Count to 3 Hello, Fred! Count to 4 Hello, Fred! Count to 5 Hello, Fred! Count to 6 Hi Fred SM! Count to 7 Hello, Fred Smith! Count to 8 Hello, Fred Smit! Count to 9 Hello, Fred Smith! count to 10

    In this tutorial, we'll use the debugger to take a closer look at this application and learn about its debugging capabilities.

  2. Press the red stop button to stop the debuggerTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (7)button (TRANSPORT+F5).

  3. In a console window, press any key to close the console window.

  1. accordingF5(Debug > Start Debugging) or select the green onestart troubleshootingbutton on the debug toolbar.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (8)

    F5Start the app with a debugger attached to the app process, but right now we haven't done anything special to test the code. So the app just loads and you see the console output.

    hell! Count to 1 Hello, Fr! Count to 2 Hey Frey! Count to 3 Hello, Fred! Count to 4 Hello, Fred! Count to 5 Hello, Fred! Count to 6 Hi Fred SM! Count to 7 Hello, Fred Smith! Count to 8 Hello, Fred Smit! Count to 9 Hello, Fred Smith! count to 10

    In this tutorial, we'll use the debugger to take a closer look at this application and learn about its debugging capabilities.

  2. By pressing (TRANSPORT+F5) or select the red onestop debuggingbutton on the debug toolbar.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (9)

  3. In a console window, press any key to close the console window.

Set a breakpoint and start the debugger

  1. indetocyclethe most importantfunction, set a breakpoint by clicking the left margin in the following line of code:

    name += letter(i)

    a red circleTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (10)appears where you set the breakpoint.

    Breakpoints are one of the most basic and important features for reliable debugging. Breakpoints indicate where Visual Studio should pause running code so you can see the value of variables, memory behavior, or if a code branch is in progress.

  2. accordingF5thestart troubleshootingbuttonTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (11), the application starts and the debugger runs at the line of code where you set the breakpoint.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (12)

    A yellow arrow indicates a statement where the debugger stopped, which also stopped the application execution at the same time (the statement has not yet been executed).

    If the application is not already running,F5Start the debugger and stop at the first breakpoint. Or else,F5Continue program execution at the next breakpoint.

    Breakpoints are a useful feature when you know which line of code or section of code you want to examine in detail. For information about the different types of breakpoints you can set, such as conditional breakpoints, seeuse breakpoints.

  1. indetocyclethe most importantfunction, set a breakpoint by clicking the left margin in the following line of code:

    name += letter(i)

    A red circle appears where the breakpoint is set.

    Breakpoints are one of the most basic and important features for reliable debugging. Breakpoints indicate where Visual Studio should pause running code so you can see the value of variables, memory behavior, or if a code branch is in progress.

  2. accordingF5(Debug > Start Debugging) thestart troubleshootingbutton, the application starts and the debugger runs at the line of code where you set the breakpoint.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (13)

    A yellow arrow indicates a statement where the debugger stopped, which also stopped the application execution at the same time (the statement has not yet been executed).

    If the application is not already running,F5Start the debugger and stop at the first breakpoint. Or else,F5Continue program execution at the next breakpoint.

    Breakpoints are a useful feature when you know which line of code or section of code you want to examine in detail. For information about the different types of breakpoints you can set, such as conditional breakpoints, seeuse breakpoints.

Use step commands to navigate through code in the debugger

Most of the time we use keyboard shortcuts here, as this is a great way to quickly run the application in the debugger (corresponding menu commands are shown in parentheses).

  1. Although it was suspended ontoin the loopthe most importantmethod, pressureF11(or choosedebug > enter) advances two more timesto send a messagemethod call.

    after pressureF11Twice you should have this line of code:

    sendMessage(name, a(i))

  2. accordingF11get back into send a messagemethod.

    The yellow pointer goes ahead toto send a messagemethod.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (14)

    F11 isenterscommand and have the application execute one statement at a time. F11 is a great way to inspect the execution flow in as much detail as possible. (We'll also show you some other options for faster code browsing.) By default, the debugger skips non-user code (if you need more details, seejust my code).

    Assuming you've checkedto send a messagemethod and want to exit the method but stay in the debugger. you can use itGo outsideSeries.

  3. accordingTRANSPORT+F11(theDebug > Split).

    This command continues execution of the application (and advances the debugger) until the current method or function returns.

    you should returntoin the loopthe most importantmethod, break into send a messagemethod call.

  4. accordingF11several times until you returnto send a messageCall the method again.

  5. Press while a method call is pausedF10(or chooseTroubleshoot > Step In) once.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (15)

    Note that this time the debugger will not passto send a messagemethod.F10Push the debugger without stepping through functions or methods in your application code (the code still runs). pressingF10existsto send a messagemethod call (instead ofF11), we skip the code implementationto send a message(we may not be interested in this right now). For more information on the different methods of moving tags, seeCode navigation in the debugger.

In this article, we use keyboard shortcuts because this is a good way to quickly run the application in the debugger (corresponding commands such as menu commands are shown in parentheses).

  1. Although it was suspended ontoin the loopthe most importantmethod, pressureF11(or choosedebug > enter) advances two more timesto send a messagemethod call.

    after pressureF11Twice you should have this line of code:

    sendMessage(name, a(i))

  2. accordingF11get back into send a messagemethod.

    The yellow pointer goes ahead toto send a messagemethod.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (16)

    F11Isenterscommand and have the application execute one statement at a time.F11is a great way to inspect the execution flow in as much detail as possible. (We'll also show you some other options for faster code browsing.) By default, the debugger skips non-user code (if you need more details, seejust my code).

    Assuming you've checkedto send a messagemethod and want to exit the method but stay in the debugger. you can use itGo outsideSeries.

  3. accordingTRANSPORT+F11(theDebug > Split).

    This command continues execution of the application (and advances the debugger) until the current method or function returns.

    you should returntoin the loopthe most importantmethod, break into send a messagemethod call.

  4. accordingF11several times until you returnto send a messageCall the method again.

  5. Press while a method call is pausedF10(or chooseTroubleshoot > Step In) once.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (17)

    Note that this time the debugger will not passto send a messagemethod.F10Push the debugger without stepping through functions or methods in your application code (the code still runs). pressingF10existsto send a messagemethod call (instead ofF11), we skip the code implementationto send a message(we may not be interested in this right now). For more information on the different methods of moving tags, seeCode navigation in the debugger.

Use the "Run to Click" navigation code.

  1. accordingF5Proceed to the turning point again.

  2. In the code editor, scroll down and hoverconsole.WriteLinemethod ito send a messagemethod until greenrun to clickbuttonTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (18)appears on the left. The button's tooltip says "Run to here".

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (19)

    noting

    Thisrun to clickButtons are new in Visual Studio 2017. (If you don't see the green arrow button, useF11In this example, the debugger is moved to the correct location. )

  3. clickrun to clickbuttonTutorial: Debugging Visual Basic Code - Visual Studio (Windows) (20).

    The debugger proceeds toconsole.WriteLinemethod.

    Using this button is equivalent to setting a temporary breakpoint.run to clickIt is convenient to quickly browse the visible area of ​​\u200b\u200bthe application code (you can click on any open file).

  1. accordingF5Proceed to the turning point again.

  2. In the code editor, scroll down and hoverconsole.WriteLinemethod ito send a messagemethod until greenrun to clickThe buttons appear on the left. The button's tooltip says "Run to here".

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (21)

  3. chooserun to clickbutton.

    The debugger proceeds toconsole.WriteLinemethod.

    Using this button is equivalent to setting a temporary breakpoint.run to clickIt is convenient to quickly browse the visible area of ​​\u200b\u200bthe application code (you can click on any open file).

Quickly restart your app

clickrestart Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (22)button on the debug toolbar (control key+TRANSPORT+F5).

when you pressrestart, which saves time compared to stopping the application and restarting the debugger. The debugger stops at the first breakpoint encountered during code execution.

The debugger stops again at the breakpoint you set earlier in the programtoring.

To restart your app, tapcontrol key+TRANSPORT+F5key combination, which saves time compared to stopping the application and restarting the debugger. The debugger stops at the first breakpoint encountered during code execution.

The debugger stops again at the breakpoint you set earlier in the programtoring.

Inspect variables using data hints

The ability to inspect variables is one of the most useful features of a debugger, and there are several ways to do it. Often when you're trying to debug a problem, you're trying to figure out if variables are storing the value you expect them to have at a particular time.

  1. at break timename += letter[i]mouse pointer overlettervariable, you will see its default value, the value of the first element of the array,"f" c.

  2. Then hold the mouse cursor overNamevariable, you will see its current value, an empty string.

  3. accordingF5(theerror correction>To continue) enough times to repeat several timestoloop, stop again at the breakpoint and hover overNameEach time the value of the variable is checked.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (23)

    The value of the variable changes with each iterationtoloop , show valueeat, AfterwardFR, AfterwardFrey, etc.

    When you fail, you often need a quick way to inspect property variable values ​​to see if they store the value you expect, and data hints are a great way to do that.

The ability to inspect variables is one of the most useful features of a debugger, and there are several ways to do it. Often when you're trying to debug a problem, you're trying to figure out if variables are storing the value you expect them to have at a particular time.

  1. at break timename += letter[i]mouse pointer overlettervariable, you will see its default value, the value of the first element of the array,"f" c.

  2. Then hold the mouse cursor overNamevariable, you will see its current value, an empty string.

  3. accordingF5(theerror correction>To continue) enough times to repeat several timestoloop, stop again at the breakpoint and hover overNameEach time the value of the variable is checked.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (24)

    The value of the variable changes with each iterationtoloop , show valueeat, AfterwardFR, AfterwardFrey, etc.

    When you fail, you often need a quick way to inspect property variable values ​​to see if they store the value you expect, and data hints are a great way to do that.

Examine the variables using the Autos and Locals windows

  1. look at (thisit wasThe window at the bottom of the code editor.

    If disabled, enable it when debugging is paused by selectingerror correction>Windows>it was.

    indeit waswindow, you can see the variables and their current values. Thisit wasThe window displays all variables used by the current or previous line (see the documentation for language-specific behavior).

  2. Then watchthe localwindow, in the tab next to itit waswindow.

  3. expandlettervariable to display the elements it contains.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (25)

    Thisthe localThe window shows the current variablefield of application, the current execution context.

  1. look at (thisit wasThe window at the bottom of the code editor.

    If disabled, enable it when debugging is paused by selectingerror correction>Windows>it was.

    indeit waswindow, you can see the variables and their current values. Thisit wasThe window displays all variables used by the current or previous line (see the documentation for language-specific behavior).

  2. Then watchthe localwindow, in the tab next to itit waswindow.

  3. expandlettervariable to display the elements it contains.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (26)

    Thisthe localThe window shows the current variablefield of application, the current execution context.

clock set

  1. Right-click in the main code editor windowNamevariable and selectAdd to view.

    ThisI'm watchingA window opens at the bottom of the code editor. you can use itI'm watchingwindow to specify the variable (or expression) you want to focus on.

    You have now set your watch toNamevariable and you can see its value change as you move through the debugger. Unlike other resizable windows,I'm watchingThe window always shows the variables you are observing (they are grayed out when they are out of scope).

Check the call stack

  1. Although it was suspended ontocircle, clickcall stackwindow that opens by default in the lower right window.

    If disabled, enable it when debugging is paused by selectingerror correction>Windows>call stack.

  2. clickF11several times until you see the debuggerto send a messagemethod. look at (thiscall stackwindow.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (27)

    Thiscall stackThe window shows the order in which the methods and functions are called. The top line shows the current function (to send a messagemethod in this application). The second line showsto send a messageknown as fromthe most importantmethod etc.

    noting

    Thiscall stackThis window is similar to the debug perspective in some IDEs, such as Eclipse.

    The call stack is a great way to inspect and understand the execution flow of an application.

    You can double-click a line of code to view that source code, which also changes the current scope that the debugger is inspecting. This action does not advance the debugger.

    You can also use the right-click menucall stackThe window does other things. For example, you can insert a breakpoint in a specified function by advancing the debugger withrun to the markerand then go to check the source code. For more information, seeHow to: Examine the call stack.

  1. Although it was suspended ontocircle, clickcall stackwindow that opens by default in the lower right window.

    If disabled, enable it when debugging is paused by selectingerror correction>Windows>call stack.

  2. clickF11several times until you see the debuggerto send a messagemethod. look at (thiscall stackwindow.

    Tutorial: Debugging Visual Basic Code - Visual Studio (Windows) (28)

    Thiscall stackThe window shows the order in which the methods and functions are called. The top line shows the current function (to send a messagemethod in this application). The second line showsto send a messageknown as fromthe most importantmethod etc.

    noting

    Thiscall stackThis window is similar to the debug perspective in some IDEs, such as Eclipse.

    The call stack is a great way to inspect and understand the execution flow of an application.

    You can double-click a line of code to view that source code, which also changes the current scope that the debugger is inspecting. This action does not advance the debugger.

    You can also use the right-click menucall stackThe window does other things. For example, you can insert a breakpoint in a specified function by advancing the debugger withrun to the markerand then go to check the source code. For more information, seeHow to: Examine the call stack.

change execution flow

  1. accordingF11ran twiceconsole.WriteLinemethod.

  2. The debugger stops atto send a messagemethod call, use the mouse to grab the left yellow arrow (run cursor) to move up one line and return toconsole.WriteLine.

  3. accordingF11.

    restart the debuggerconsole.WriteLinemethod (you can see it in the output of the console window).

    By changing the execution flow, you can do things like try different code execution paths or re-run the code without restarting the debugger.

    warn

    Typically, you should use this feature with caution and you'll see a warning in the tooltip. You may also see other warnings. Moving the cursor cannot restore your program to a previous program state.

  4. accordingF5Go ahead and run the app.

    Congratulations on completing this tutorial!

Next step

In this tutorial, you learned how to start the debugger, step through code, and inspect variables. You may want to delve into the debugging features and links for more information.

First look at the debugger

the feedback

Submit and view comments

References

Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated: 21/09/2023

Views: 5963

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.