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.
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:
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.
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.
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.
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:
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.
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.
Removing duplicate records from SQL Table
0 comments:
Post a Comment