Maximizing everyday debugging

Exclusive offer: get 50% off this eBook here
Visual Studio 2013 Cookbook

Visual Studio 2013 Cookbook — Save 50%

Over 50 simple but incredibly effective recipes to get you up and running with the powerful features of Visual Studio 2013 with this book and ebook

$26.99    $13.50
by Jeff Martin | March 2014 | Cookbooks Enterprise Articles

Given the frequency of debugging, any refinement to the tools used can pay immediate dividends. In this article by Jeff Martin, the author of the book Visual Studio 2013 Cookbook, he explains the popular Edit and Continue feature that now supports working with 64-bit code. Added to that is the new ability to see the return value of functions in your debugger. These two features combine to make debugging code easier, allowing you to solve problems faster.

(For more resources related to this topic, see here.)

Getting ready

For this article, you will just need a premium version of VS2013 or you may use VS Express for Windows Desktop. Be sure to run your choice on a machine using a 64-bit edition of Windows. Note that Edit and Continue previously existed for 32-bit code.

How to do it…

Both features are now supported by C#/VB, but we will be using C# for our examples. The features being demonstrated are compiler-based features, so feel free to use code from one of your own projects if you prefer. To see how Edit and Continue can benefit 64-bit development, perform the following steps:

  1. Create a new C# Console Application using the default name.
  2. To ensure the demonstration is running with 64-bit code, we need to change the default solution platform.
  3. Click on the drop-down arrow next to Any CPU and select Configuration Manager…:

  4. When the Configuration Manager dialog opens, we can create a new Project Platform targeting 64-bit code. To do this, click on the drop-down menu for Platform and select <New...>:

  5. When <New...> is selected, it will present the New Project Platform dialog box. Select x64 as the new platform type:

  6. Once x64 has been selected, you will return to the Configuration Manager. Verify that x64 remains active under Platform and then click on Close to close this dialog. The main IDE window will now indicate that x64 is active:

  7. Now, let's add some code to demonstrate the new behavior. Replace the existing code in your blank class file so that it looks like the following listing:

    class Program { static void Main(string[] args) { int w = 16; int h = 8; Debugging Your .NET Application 156 int area = calcArea(w, h); Console.WriteLine("Area: " + area); } private static int calcArea(int width, int height) { return width / height; } }

  8. Let's set some breakpoints so that we are able to inspect during execution. First, add a breakpoint to the Main method's Console line. Add a second breakpoint to the calcArea method's return line. You can do this by either clicking on the left side of the editor window's border or by right-clicking on the line, and selecting Breakpoint | Insert Breakpoint:

  9. If you are not sure where to click, use the right-click method and then practice toggling the breakpoint by left-clicking on the breakpoint marker. Feel free to use any method that you find most convenient.

    Once the two breakpoints are added, Visual Studio will mark their location as shown in the following screenshot (the arrow indicates where you may click to toggle the breakpoint):

  10. With the breakpoint marker now set, let's debug the program. Begin debugging by either pressing F5 or clicking on the Start button on the toolbar:

  11. Once debugging starts, the program will quickly execute until stopped by the first breakpoint. Let's first take a look at Edit and Continue. Visual Studio will stop at the calcArea method's return line. Astute readers will notice an error (marked by 1 in the following screenshot) present in the calculation as the area value returned should be width * height. Make the correction.
  12. Before continuing, note the variables listed in the Autos window (marked by 2 in the following screenshot). If you don't see Autos, it can be made visible by pressing Ctrl + D, A or through Debug | Windows | Autos while debugging.

  13. After correcting the area calculation, advance the debugging step by pressing F10 twice. (Alternatively make the advancement by selecting the menu item Debug | Step Over twice). Visual Studio will advance to the declaration for area. Note that you were able to edit your code and continue debugging without restarting.
  14. The Autos window will update to display the function's return value, which is 128 (the value for area has not been assigned yet):

There's more…

Programmers who write C++ already have the ability to see the return values of functions; this just brings .NET developers into the fold. Your development experience won't have to suffer based on the languages chosen for your projects.

The Edit and Continue functionality is also available for ASP.NET projects. New projects created in VS2013 will have Edit and Continue enabled by default. Existing projects imported to VS2013 will usually need this to be enabled if it hasn't been already. To do so, right-click on your ASP.NET project in Solution Explorer and select Properties (alternatively, it is also available via Project | <Project Name> Properties…). Navigate to the Web option and scroll to the bottom to check the Enable Edit and Continue checkbox. The following screenshot shows where this option is located on the properties page:

Summary

In this article, we learned how to use the Edit and Continue feature. Using this feature enables you to make changes to your project without having to immediately recompile your project. This simplifies debugging and enables a bit of exploration. You also saw how the Autos window can display the values of variables as you step through your program’s execution.

Resources for Article:


Further resources on this subject:


Visual Studio 2013 Cookbook Over 50 simple but incredibly effective recipes to get you up and running with the powerful features of Visual Studio 2013 with this book and ebook
Published: March 2014
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


Jeff Martin

Jeff Martin is a long-time technology enthusiast and has been a contributing writer for InfoQ (www.infoq.com) for over two years, focusing on .NET and Microsoft-based platforms. Having experience in writing, testing, and designing software, he enjoys learning about new technologies and explaining them to a broader audience.

Readers are encouraged to follow @VSDevTips on Twitter to receive updates on the book as well as news for getting the most out of Visual Studio.

Books From Packt


Visual Studio 2012 Cookbook
Visual Studio 2012 Cookbook

Reporting with Visual Studio and Crystal Reports
Reporting with Visual Studio and Crystal Reports

Software Testing using Visual Studio 2012
Software Testing using Visual Studio 2012

Visual Studio 2010 Best Practices
Visual Studio 2010 Best Practices

Software Testing using Visual Studio 2010
Software Testing using Visual Studio 2010

 Visual Studio 2013 and .NET 4.5 Expert Cookbook
Visual Studio 2013 and .NET 4.5 Expert Cookbook

Refactoring with Microsoft Visual Studio 2010
Refactoring with Microsoft Visual Studio 2010

Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
Visual Studio 2012 and .NET 4.5 Expert Development Cookbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software