|
1 | 1 | --- |
2 | 2 | title: "Debugging code for absolute beginners" |
3 | 3 | description: "If you're debugging for the first time, learn a few principles to help you run your app in debugging mode with Visual Studio." |
4 | | -ms.date: 10/19/2023 |
| 4 | +ms.date: 12/5/2024 |
5 | 5 | ms.topic: tutorial |
6 | 6 | helpviewer_keywords: |
7 | 7 | - "debugger" |
@@ -309,7 +309,7 @@ Maffei 1, 11, ConsoleApp_FirstApp.GType |
309 | 309 |
|
310 | 310 | 1. With the app still running, insert a breakpoint. |
311 | 311 |
|
312 | | - Right-click next to the `Console.WriteLine` method to get the context menu and select **Breakpoint** > **Insert Breakpoint** from the fly-out menu. |
| 312 | + In the `foreach` loop, right-click next to the `Console.WriteLine` method to get the context menu and select **Breakpoint** > **Insert Breakpoint** from the fly-out menu. |
313 | 313 |
|
314 | 314 | #### [C#](#tab/csharp) |
315 | 315 |
|
@@ -339,26 +339,36 @@ Maffei 1, 11, ConsoleApp_FirstApp.GType |
339 | 339 |
|
340 | 340 | 1. Hover over the `GalaxyType` variable on the right, and then, to the left of the wrench icon, expand `theGalaxy.GalaxyType`. You see that `GalaxyType` contains a property `MyGType`, and the property value is set to `Spiral`. |
341 | 341 |
|
342 | | - :::image type="content" source="../debugger/media/beginners-inspect-variable.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code in yellow and a menu open below the Galaxy GalaxyType property."::: |
| 342 | + :::image type="content" source="../debugger/media/beginners-inspect-variable.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code in yellow and a menu open below the Galaxy GalaxyType property." lightbox="../debugger/media/beginners-inspect-variable.png"::: |
343 | 343 |
|
344 | 344 | "Spiral" is actually the correct value you were expecting to print to the console! So it's a good start that you can access the value in this code while running the app. In this scenario, we're using the incorrect API. Let's see if you can fix this while running code in the debugger. |
345 | 345 |
|
346 | | -1. In the same code, while still debugging, put your cursor at the end of `theGalaxy.GalaxyType` and change it to `theGalaxy.GalaxyType.MyGType`. Although you can make the change, the code editor shows you an error indicating it can't compile this code. (In Visual Basic, the error isn't shown and this section of code works.) |
| 346 | +1. In the same code, while still debugging, put your cursor at the end of `theGalaxy.GalaxyType` and change it to `theGalaxy.GalaxyType.MyGType`. Although you can make the edit, the code editor shows you an error (red squiggly line). (In Visual Basic, the error isn't shown and this section of code works.) |
347 | 347 |
|
348 | 348 | 1. Press **F11** (**Debug** > **Step Into** or the **Step Into** button in the Debug Toolbar) to execute the current line of code. |
349 | 349 |
|
350 | 350 | **F11** advances the debugger (and executes code) one statement at a time. **F10** (**Step Over**) is a similar command, and both are useful when learning how to use the debugger. |
351 | 351 |
|
| 352 | + ::: moniker range=">= vs-2022" |
| 353 | + |
| 354 | + When you try to advance the debugger, the Hot Reload dialog box appears, indicating that edits can't be compiled. |
| 355 | + |
| 356 | + :::image type="content" source="../debugger/media/vs-2022/beginners-edit.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code highlighted in red and a message box with the Edit option selected."::: |
| 357 | + ::: moniker-end |
| 358 | + |
| 359 | + ::: moniker range="<= vs-2019" |
| 360 | + |
352 | 361 | The Edit and Continue dialog box appears, indicating that edits can't be compiled. |
353 | 362 |
|
354 | | - :::image type="content" source="../debugger/media/beginners-edit.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code highlighted in red and a message box with the Edit option selected."::: |
| 363 | + :::image type="content" source="../debugger/media/beginners-edit.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code highlighted in red and a message box with the Edit option selected."::: |
| 364 | + ::: moniker-end |
355 | 365 |
|
356 | 366 | > [!NOTE] |
357 | 367 | > For debugging the Visual Basic example code, skip the next few steps until you're instructed to click the **Restart** :::image type="content" source="../debugger/media/dbg-tour-restart.png" alt-text="Icon showing Restart app button in Debug toolbar."::: button. |
358 | 368 |
|
359 | | -1. Select **Edit** in the **Edit and Continue** message box. You see an error message now in the **Error List** window. The error indicates that the `'object'` doesn't contain a definition for `MyGType`. |
| 369 | +1. Select **Edit** in the **Hot Reload** or **Edit and Continue** message box. You see an error message now in the **Error List** window. The error indicates that the `'object'` doesn't contain a definition for `MyGType`. |
360 | 370 |
|
361 | | - :::image type="content" source="../debugger/media/beginners-no-definition.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code highlighted in red and an Error List window with two errors listed."::: |
| 371 | + :::image type="content" source="../debugger/media/beginners-no-definition.png" alt-text="Screenshot of the Visual Studio Debugger with a line of code highlighted in red and an Error List window with two errors listed." lightbox="../debugger/media/beginners-no-definition.png"::: |
362 | 372 |
|
363 | 373 | Even though we set each galaxy with an object of type `GType` (which has the `MyGType` property), the debugger doesn't recognize the `theGalaxy` object as an object of type `GType`. What's going on? You want to look through any code that sets the galaxy type. When you do this, you see that the `GType` class definitely has a property of `MyGType`, but something isn't right. The error message about `object` turns out to be the clue; to the language interpreter, the type appears to be an object of type `object` instead of an object of type `GType`. |
364 | 374 |
|
|
0 commit comments