Upload
sikandar-kumar
View
7
Download
2
Embed Size (px)
DESCRIPTION
.NET Tutorials_ Debugging in Vs
Citation preview
6/29/2014 .NET tutorials: Debugging in VS.NET
http://www.dotnetspider.com/tutorials/DotNet-Tutorial-280.aspx 1/4
Interview Experience | New Members | IT Companies | Peer Appraisal | Members | Revenue
Sharing | New Posts | Social |
Active Members
Today
narasiman (5)
Last 7 Days
Pawan Awasthi
(82)
Asheej T K (58)
naveensanagase...
(37)
more...
Tutorials»DotNet tutorials»
Debugging in VS.NET
In this tutorial, you can learn more about debugging in Visual Studio, how to
use the debugging feature and how to effectively debug an application to
troubleshoot coding issues.
If you make a syntax error, then the compiler will tell you and you can easily solve
the issue. But what if there is a logical error? You may never know what went
wrong, just by looking into the code. During runtime, the application may give wrong
results, which may not be noticed immediately by anyone. When someone report
that your "calculator program" gives the result 10 when adding 5 and 4, what will
you do? A logical error in a small calculator program may be an easy to fix issue, but
what if it is a very complex accounting software?
The Debugger becomes a very handy tool in such situations. A Debugger is a
software process that help you monitor the execution of your code. Microsoft gives
us a very powerful debugger, integrated with the VS.NET. When you execute your
code in debug mode, you can watch how it executes each line of code. You can run
(debug) the application step by step, line by line and see the value of each variable
at any point of time. You can visually count how many times a whileloop executes
and see whether it executes the ifblock or else block.
So, in short, debugger help you watch how your code is executed. You can see how
your calculator program calculates 5 + 4 = 10 and easily figure out the logical error.
Debugging your project
Running your project in Debug mode doesn't need lot of work. Just choose the
menu : Debug > Start or press F5. Now your application will run in debug mode. To
run your application in non-debug mode, choose the menu : Debug > Start without
Login Register
Search
Tutorials Forum Career Development Articles Reviews Jobs Practice Tests Projects
Code Converter
6/29/2014 .NET tutorials: Debugging in VS.NET
http://www.dotnetspider.com/tutorials/DotNet-Tutorial-280.aspx 2/4
Debugging or press Ctrl F5.
You may not feel any difference whether you run in debug mode or non-debug mode.
In both the cases, your application runs as usual! To find the difference, you can
use breakpoints
.BreakPoints
BreakPoints are used to stop the execution at certain lines of code in the application
and monitor the values of various variables etc at that point of time. To put
breakpoints, click on any line of code and press F9or click on the leftbar of any line
of code. When a breakpoint is enabled, the line will be highlighted with BROWN color
and a BROWN circle will appear on the left bar (See image below). To remove a
breakpoint, press F9 again.
Open your project and mark breakpoints in different lines of code. Now start the
debugger by pressing F5. Note that every time the execution passes through the
lines marked as 'breakpoint', execution stops there. You can press F5 to continue
the execution again. Or, press F10 to continue the execution line by line! When the
execution stops on a breakpoint, or when you continue execution line by line by
pressing F10, you can point your mouse over any variable and it will show you the
value of that variable at that point of time. You can see how the values are changed
at each line of code and this helps you easily figure out the logical errors. This
process is called 'Debugging'. Next time, when you hear someone complaining about
his 'debugging nightmares', you know what it is!
Change path of execution
When the execution reaches a breakpoint, you can see a little YELLOW Arrow on the
leftbar of the current line of code and the line will be highlighted with YELLOW color
(See image below). When you press the F10, the execution will proceed to the next
line of code.
You can change this execution path by clicking on the yellow arrow on the leftbar
and dragging it into any other line of code. For example, if you are currently inside
an if block, you can drag the execution to the else block (or, to some other line of
code), thus changing the application behaviour the way you want! Don't you agree
that the Debugger is a very powerful tool?
Quick Watch
While debugging, you can view the value of any variable instantly by pointing the
6/29/2014 .NET tutorials: Debugging in VS.NET
http://www.dotnetspider.com/tutorials/DotNet-Tutorial-280.aspx 3/4
mouse over the variable (See image below. You can see a very small light yellow
window showing name = "Little John". This is displayed when the mouse is pointed
over the variable name). But this will work only for simple types like int, string etc.
In case of types like DataTable, DataSet, ArrayList etc, you can right click on the
object name and choose Quick Watch. A small window will open and will show you
the complete object. You can expand the properties of the object and view the
values of each proeprty. In case of a DataSet, you can Quick Watch the dataset
and expand each table inside the dataset, each row in each table, each column in
each row etc. Thus the Quick Watch will help you view the values of the 'complete
object'.
Watch Window
When you debug the application and step through each line, it is not really
necessary to point the mouse on each variable to view the value. Just add the
variable to the 'Watch Window' and the values are always visible in the little 'Watch
Window' in the bottom of your VS.NET (see image below. The variable nameis added
to the watch window). To add any variable to Watch Window, righ click on the
variable and choose 'Add Watch'. You can choose the same option from the 'Quick
Watch' window also.
The 'Watch Window' may be minimized by default. You can point your mouse on the
'Watch' icon in the bottom of the VS.NET to expand this window. Use the 'push
button' in the watch window to keep it always expanded.If 'Watch Window' is not
visible in the bottom, select the menu option from VS.NET main menu : Debug >
Windows > Watch > Watch 1
System.Diagnostics.Debug.WriteLine
You may use System.Diagnostics.Debug.WriteLineto print any values into the
output window while debugging.
Ex:
System.Diagnostics.Debug.WriteLine( "*** My name is : " + name );
System.Diagnostics.Debug.WriteLine( "+++ See the Output window now." );
System.Diagnostics.Debug.WriteLine( "=== End." );
See the image below to see how it appears in the output window.
Advanced debugging features
6/29/2014 .NET tutorials: Debugging in VS.NET
http://www.dotnetspider.com/tutorials/DotNet-Tutorial-280.aspx 4/4
Advanced debugging features
The debugger comes with several other advanced features like conditional
breakpoints, CallStack etc. You may find more information from MSDN or other web
sites. To keep it simple and helpful for beginners, we have mentioned only the
commonly used features of VS.NET debugger.
Next Chapter: Accessing Database
Previous Chapter: Application Configuration Files
More Chapters: .NET Tutorials
More Tutorials: Tutorial Index