Debugging a piece of code is always difficult and it is more difficult when you are debugging on someone else’s code. In all such cases, you will need to set the breakpoints and check the values that it returns before proceeding next.

 

Visual Studio 2015 makes it easier by providing you an option to call a specific method once you reach the breakpoint and based on requirement, print something on the output stream.

 

How to call a method when Visual Studio 2015 debugger hits? (www.kunal-chowdhury.com)

 

Today we are going to learn about Visual Studio 2015 debugging tips and the way to handle breakpoints with enhanced practices. We will learn how to call a method when the debugger in Visual Studio 2015 hits.

 

To demonstrate this, let us create a new console project and write a method “PrintMessage” having no parameters. The method will just print a line in the console output. Now inside the Main method, add a breakpoint and open the breakpoint settings as shown below:

 

1. Open breakpoint settings in Visual Studio 2015 (www.kunal-chowdhury.com)

 

The breakpoint settings tab will have two checkboxes labelled “Conditions” and “Actions”. For our demonstration, check the box labelled “Actions”. This will provide you an input box to enter the log message. This box also accepts members as input and can be written inside “{  }” braces.

 

2. Create custom action for breakpoint in Visual Studio 2015 (www.kunal-chowdhury.com)

 

Here we are going to call the PrintMessage method when the breakpoint hits. So, call the method inside the curly braces. The intellisense will show you a list will all the available options based on the context of what you are typing.

 

3. Call predefined method on breakpoint actions in Visual Studio 2015 (www.kunal-chowdhury.com)

 

Do you know that I have a dedicated page for Visual Studio 2015 Tips & Tricks?

 

This will look like the below screenshot. As the method does not accept any parameter, keep the call as-is and click on the close button. Now when you run the program in debug mode and hit that breakpoint, it will call the said method automatically. Check the “Continue execution” to continue with the next calls even though the breakpoint hits.

 

4. Call a method when a breakpoint hits in Visual Studio 2015 (www.kunal-chowdhury.com)

 

Have a look into the symbol of the breakpoint. In general the breakpoint symbol is set to circle but when you set the action for the breakpoint, it changes it’s shape to diamond.

 

You can also call the method passing some value to it as parameter. For example, let us pass an integer value to the “PrintMessage” method. Change the signature of the method and go to the breakpoint settings as shown in the below screenshot:

 

5. Open the breakpoint settings in Visual Studio 2015 (www.kunal-chowdhury.com)

 

Now in the breakpoint settings, under the “Actions” panel, change the method call and pass the desired value. To demonstrate it, we passed the integer value ‘x’ as shown below. Click enter to accept the changes and click close to hide the settings page.

 

6. Passing a parameter to method when breakpoint hits in Visual Studio 2015 (www.kunal-chowdhury.com)

 

Now when you run the program and hit the breakpoint, the same method will be called passing the value as the parameter, which you can use inside the method as per your need.

 

Hope you liked this debugging tips. Don’t forget to check my other posts on Visual Studio 2015 Tips & Tricks available in this URL:  http://visual-studio-2015.kunal-chowdhury.com.

 

Do share this post and leave your feedback below. If you know about some other tips on Visual Studio 2015, do share it with us. Connect with me on the social networking channels and subscribe to my feed to get all the updates that I share over there.

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.