|
5.5. Debug .NET ComponentsIf you've been following along with the preceding example by writing code in Excel and Visual Studio .NET, you've probably noticed that you can't build the .NET project while Excel has a reference to that project's type library. You need to close Excel or remove the reference each time you make a change in the .NET project. That's because Visual Studio .NET can't overwrite the type library while another application is using it. This makes debugging .NET components from Excel difficult. In fact, it's not a very good practice. It is a much better practice to add a second test project to your .NET component solution and make that project the start-up project. Note: Building tests into your development projects is a good practice. In fact, there's a schoolof thought (called extreme programming) that says you should write tests first, before you implement any feature. 5.5.1. How to do itTo add a test project to the NetForExcel sample, follow these steps:
For example, the following code tests the NetString class from the NetForExcel component created earlier: ' .NET test code Module Module1 Dim WithEvents NetStr As New NetForExcel.NetString Sub Main( ) Dim ar( ) As String = {"This", "That", "Other"} Dim ar2(1, 1) As String, str As String = "Some more text" ar2(0, 0) = "This" : ar2(0, 1) = "That" : ar2(1, 0) = "Other" Console.WriteLine(NetStr.Join(ar, ", ")) Console.WriteLine(NetStr.Sort(str, False)) ' Cause error. Try 'NetStr.Join("Test, That, Other") 'NetStr.Join(ar2) NetStr.Split(str, " r") Catch ex As Exception Console.WriteLine("Error: " & ex.Message) End Try ' Wait for Enter keypress to end. Console.Read( ) End Sub Private Sub NetStr_Sorted(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles NetStr.Sorted Console.WriteLine("Sort event complete. Result: " & NetStr.Value) End Sub End Module If you run the NetForExcel solution from Visual Studio .NET by pressing F5, Windows starts the test project and displays the results in a console window as shown in Figure 5-7. Figure 5-7. Using a console test project to debug a .NET component before using it from ExcelNow, you can use Visual Studio .NET's debugging tools to step into procedures, set breakpoints and watches, and perform other typical debugging and testing tasks. |
|