Debugging at Run Time


The second method of finding out what's going on in your code is to debug it. This allows you to step through the code line by line, and even examine the contents of variables. Many people consider debugging to be an arcane art, but it's actually really simple – you just have to know how to do it – and since it's so simple, let's dive right in.

The example overleaf again requires that an IIS Application is created, called Wrox.

Try It Out —Debugging a program

  1. Create a new ASP.NET Page called debugging.aspx.

  2. Add a Button and a Label to the page.

  3. Double-click the button to show the Code view.

  4. Add the following code:

    Sub Button1_Click(sender As Object, e As EventArgs)  Dim i As Integer  Dim j As Integer  Dim Sum As Integer  i = 3  j = 4  Sum = i + j  Label1.Text = Sum End Sub
  5. Switch to All view, and add the debugging attribute to the top of the page:

     <%@ Page Language="VB" Debug="true" %> 
  6. Save the page and navigate to it from your browser. Press the button to prove it works OK.

  7. From Windows Explorer, navigate to C:\Program Files\Microsoft.NET\FrameworkSDK\GuiDebug. If you've installed Visual Studio.NET instead of just the .NET Framework, then substitute Microsoft Visual Studio.NET for Microsoft.NET in the path.

  8. Double-click DbgCLR.exe to launch the debugger.

  9. From the Tools menu select Debug Process….

    click to expand

  10. Click Attach…, and then click Close.

  11. From the File menu select Open and File.

  12. Navigate to C:\wrox and open debugging.aspx.

  13. Click on the gray border at the left, next to the i = 3 line of code you entered:

    click to expand

    This sets a breakpoint on that line. A breakpoint is a line where the processing of the code will halt temporarily in the debugger.

  14. Switch back to your browser, and click the button again. Notice how you are switched to the debugger, with the line highlighted. Also notice the Locals window at the bottom, showing the values of our three variables.

  15. Press F11 three times, watching how the current line changes, as do the values of our variables.

  16. Press F5 to continue. The page will now complete execution, and the browser will be re-displayed.

It's worth having a look at the Debug menu while debugging to see what other things you can do – there are tools in here that will enable you to step over a function rather than into it, clear breakpoints and so on. We just used the Step Into command to step through our code, and there is no complex code here. But, if you've got functions and branches (If ... Then statements), then the debugger follows each line as it executes. This gives you a great way of understanding the flow of your program, and combined with the ability to see what values your variables contain, it's invaluable in tracking down problems.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net