Home Page

Debugging your C# program consists of monitoring the execution of the source code as the program is running.  Visual Web Developer has a debugger full of useful features.  The main point of debugging your source code is to determine whether or not it is behaving the way that you expected when you programmed it.  Not only can you go through the code to find problems or fix bugs, but you can also use the debugger to validate that the code is behaving exactly as you had intended it.  This is especially useful in more complicated blocks of code or blocks of code that rely on external resources to make a decision.  A good example is with database programming, where you may not have any idea ahead of time what the data will look like ahead of time, or whether certain fields will contain nulls, so you can use the debugger as your program runs to walk through different scenarios.  I want to make the point that the debugger is not just for situations where something is broken and you can use it for so much more than that.

Using the debugger is very easy.  You first need to determine what part of your program you want to examine.  Then you choose a line of code that you want the debugger to stop at and set what is called a “breakpoint”.  A breakpoint is a line of code in your program that the .NET engine knows to stop at when the program is running.  When the program actually runs, the .NET engine will stop the program at the breakpoint, minimize the web browser and open up the code editor window at the line where the breakpoint resides.  The breakpoint line will be highlighted by the debugger so that you know the program has stopped there.

Once the debugger has stopped at the breakpoint, you can use the debugger to do a number of things.  You can look at the values of the variables in your code by placing the cursor directly over the variable in the code.  The debugger also lists all the variables in your code inside the Locals window and you can use the to see the values of the variables as well.

To move to the next line of code you must use the Step Over button in the toolbar or press the F10 key.  You can keep stepping over each line of code and examine how the program is behaving.  If you want to stop debugging and go back to running the program just press the play button which will have the tooltip of “Continue” when the debugger is running.  All of the debugger functions such as Step Over and Continue are also available from the Debug menu.  Click here to see a sample video demonstrating how to use the debugger.

In C#, comments are blocks of code that are marked with the characters “//” and are ignored by the compiler.  Comments are used to either put documentation into your program or make some lines of code inactive.  For example you may want to add a comment above some of your source code that describes specifically what that part of the program does.  This is very useful even for yourself so that when you come back to your source code later, you can quickly remember why or how you programmed something.  Another example of comment use is to mark some of your lines of code as inactive so that the compiler ignores them.

To create comments you have two options.  You can type “//” on a blank line in the code editor and then type whatever text you want following that to complete the comment.  You can also highlight a block of source code and push the comment button on the toolbar at the top of Visual Web Developer.  The comment button tooltip says “Comment out the selected lines”.  There is also an uncomment button next to it which does the reverse and removes the comment characters from a block of code.  When you use the comment button in the toolbar, all it does is add the “//” characters to the beginning of each line of code that you have highlighted.  Click here to watch a sample video where I add comments to my program using both techniques.  Notice that I also use the uncomment button.

Here are some tips and techniques for using comments:

  • A very common way to use comments is to place them above variable declarations.  We will see examples of this usage of comments in a later lesson entitled “Data types lab exercise”.
  • When you are coding a complicated program and you need to compile the application, but there are sections that you are unsure about.  You can comment those sections out temporarily and perform a compile.  This is especially handy if you have an idea in your head and you want to start programming it, but you don’t have time to finish it so you temporarily comment it out so that you don’t lose the idea.
  • If you are programming a module and you are referring to an example from a book or website, you can paste the example code into your program and comment it out so that the compiler can ignore it.  I use this technique a lot and I recommend that beginning programmers do that as well.  Sometimes I permanently leave the reference material as comments in my program just so that I can keep track historically of where I got it from. 
« Previous PageNext Page »