Showing posts with label Tips & Tricks. Show all posts
Showing posts with label Tips & Tricks. Show all posts

Saturday, November 21, 2015

Generate UML Sequence diagram from Visual Studio

Yes it is correct, you can generate Sequence diagrams for your code, from within the Visual Studio and that too automatically.No need to create it your self. Let's see how we can do this.

Let's create a new sample project. Let's add a static classes Class1 and static method TestMethod1 in this class. Now let's create a test method on Default.aspx and call it on the Page_Load event. This function, in turn, will call TestMethod1 of Class1. So our code will look like the following:



Now, right click on the Page_Load event, and select the option "Generate Sequence Diagram". 



This will open up a window to define the settings to be used for generating this diagram. You can set what level of code it should consider for generating the diagram, whether to include the code within the entire solution or current project only etc.

Also, you can specify whether to save the diagram in current project or not. So we increase the call depth to 2 and keep rest of the settings as it is. 



Click "Ok" and it will generate the sequence diagram for the code we have.  


So the sequence diagram is ready. Just increase the "Maximum Call Depth" property and create the diagram to understand the code in easily. Happy Coding...!!!

Saturday, July 11, 2015

Function breakpoints in Visual Studio

Previously we discussed the concept of Hit Count breakpoints and Conditional breakpoints.  We will now discuss the concept of Function breakpoints. This functionality allows us to set the name of any function as the condition for the breakpoint to hit.
Let's start by creating 2 different functions SetString() and GetString() and call them as SetString() first and then GetString(). 

To set the breakpoints for this type, go to Debug -> Windows -> Breakpoints to open the Breakpoints window. Select the option New -> Break at Function option from the window.

This open ups a new window and we specify the name of the GetString() function as the function condition for the breakpoint to be hit.

Hover the breakpoint and we can see its details.

Run the application and breakpoint will get hit only when the execution of the Getstring() function starts.

Easy and helpful, isn't it. Happy coding...!!!

Sunday, July 5, 2015

Hit Count breakpoints in Visual studio

Previously in our discussion, we discussed about the concept of Conditional breakpoints. In this article, we will discuss about the Hit Condition breakpoints.

Consider a case where you have a for loop with iteration of thousands of records. You know that your records at around 500th location is corrupted and you need to debug that value. Will you sit and keep pressing F5 until the record number 500 is reached ? This is where you can use the concept of Hit Condition breakpoints. This is achieved by setting Hit Count condition on the breakpoint i.e. hit the breakpoint when it has iterated 499 times.

Let's create  a sample application to understand this.  Add a for loop with iteration of 1000 records and add a condition to hit the breakpoint when the 500th record is being iterated.
To add the condition, right click on the breakpoint and select the 'Hit Count' condition. This will open up a window where we can define a condition to hit the breakpoint only when it meets the criteria set for its execution.







Here we have 4 different conditions and these are self explanatory in their details. So we select the condition 'break when the hit count is equal to' and set its value to be 500.

Hover to the breakpoint and see the details:


Run the application and see the value of variable 'i', when the breakpoint is hit.



So here when the value of i becomes 499 (starting from 0), it is iterating the 500th record and the breakpoint gets hit. So set the break point and enjoy the debugging...!!!

Saturday, July 4, 2015

Conditional Breakpoints in Visual Studio

This is just a simple trick which many of you might have been using. It's a small but very powerful tip, especially helpful when you are debugging large code base. This is the concept of Breakpoint Conditions.
As the name itself suggests, it is setting the breakpoints which are hit, only when certain condition is met. It's easy to implement as well. Create a new application and set a normal breakpoint.



So this will be hit every-time the Page_Load executes. This could be very annoying when you are debugging for a bug and this keeps on hitting this breakpoint. So let's make it conditional. Simply put your mouse on the breakpoint, right click and select 'Condition'.




This will open up a window where we can define a condition to hit the breakpoint only when the condition is met. Even you get the intellisense help for it.



See how the color of the breakpoint changes with a plus sign on it. Also, hover over it and you can see the details.




Now let's run the application. First time, no breakpoint is hit. Press the button for postback and see the breakpoint gets executed.




Even you can set this condition to be on your variables or properties etc.  So happy debugging now...!!!