Section 1.2. Code, Debug, and Continue Without Restarting Your Application


1.2. Code, Debug, and Continue Without Restarting Your Application

Visual Basic 6 developers are accustomed to making changes on the fly, tweaking statements, refining logic, and even inserting entirely new blocks of code while they work. But the introduction of a new compile-time architecture with the .NET 1.0 common language runtime (CLR) caused this feature to disappear from Visual Studio .NET 2002 and 2003. Fortunately, it's returned in Visual Basic 2005, with a few enhancements and one major caveatit won't work with ASP.NET.


Note: The single most requested feature from VB 6 returns to . NET: a debugger that lets you edit code without restarting your application.

1.2.1. How do I do that?

To see edit-and-debugging at its simplest, it's worth looking at an example where a problem sidelines your codeand how you can quickly recover. Figure 1-3 shows a financial calculator application that can calculate how long it will take you to become a millionaire, using Visual Basic's handy Pmt( ) function.

Figure 1-3. A simple form for a financial calculation


To create this program, first add four text boxes (the labels are optional), and then name them txtInterestRate, txtYears, txtFutureValue, and txtMonthlyPayment (from top to bottom). Then, add a button with the following event handler:

Private Sub btnCalculate_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnCalculate.Click          Dim InterestRate, Years, FinalValue, MonthlyPayment As Double     InterestRate = Val(txtInterestRate.Text)     FinalValue = Val(txtFutureValue.Text)     MonthlyPayment = Pmt(InterestRate / 12 / 100, _         Years * 12, 0, -FinalValue, DueDate.BegOfPeriod)     txtMonthlyInvestment.Text = MonthlyPayment.ToString("C")      End Sub

Now run the application, enter some sample values, and click the button. You'll receive a runtime exception (with the cryptically worded explanation "Argument NPer is not a valid value") when your code tries to calculate the MonthlyPayment value. One way to discover the source of the problem is to move the mouse over all the parameters in the statement and verify that they reflect what you expect. In this case, the problem is that the Years variable is never set, and so contains the value 0.

Thanks to edit-and-continue debugging, you can correct this problem without restarting your application. When the error occurs, click the "Enable editing" link in the error window. Then, add the missing line:

Private Sub btnCalculate_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles btnCalculate.Click          Years = Val(txtYears.Text)     ...      End Sub

Now, look for the yellow arrow in the margin that indicates where the debugger is in your code. Click and drag this yellow arrow up to the newly added line so that it will be executed next. When you press F5 or click the Start button, the code resumes from this point and the calculation completes without a hitch.


Note: You don't need to wait for an error to occur to use edit-and-continue debugging. You can also set a breakpoint in your code or select Debug Break from the menu at any time.

1.2.2. What about...

...changes that the edit-and-continue debugger doesn't support? For the most part, edit-and-continue debugging in Visual Basic 2005 supports a greater variety of edits than supported in Visual Basic 6. However, there are still some types of edits that require you to restart your application. One example is if you delete the method in which your code is currently executing. For the full list, refer to the MSDN help, under the index entry "Edit and Continue unsupported declaration edits" (which describes disallowed changes to declarations, like properties, methods, and classes) and "Edit and Continue unsupported property and method body edits" (which describes disallowed changes inside your actual code routines).

To alert you when you make an unsupported edit, Visual Studio underlines the declaration of the current class with a green squiggly line. If you hover over that line, a ToolTip appears that explains the offending change. Figure 1-4 shows an example.

Figure 1-4. A ToolTip explaining an unsupported edit


At this point, you can either undo this change, or continue (knowing that you'll need to restart the program). If you attempt to continue execution (by pressing F5 or F8), Visual Studio asks whether you want to stop debugging or want to cancel the request and continue editing your code.

A more significant limitation of the new edit-and-continue feature is that it doesn't support ASP.NET web applications. However, Visual Basic (and C#) developers still receive some improvement in their web-application debugging experience. Visual Studio 2005 compiles each web page separately, rather than into a single assembly (as was the model in previous versions). As a result, when you find some misbehaving code in a web page, you can pause the debugger, edit the code, and refresh the web page by clicking Refresh in your browser. This behavior gives you an experience that's similar to edit-and-continue, but it only works on a per-page basis. Unfortunately, this feature won't help you if you're in the middle of debugging a complex routine inside a web page. In that case, you'll still need to re-request the web page after you make the change and start over.



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon

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