Section 5.5.Debug .NET Components


5.5. Debug .NET Components

If 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 it

To add a test project to the NetForExcel sample, follow these steps:

  1. From Visual Studio .NET, choose File Add Project New Project. Visual Studio .NET displays the Add New Project dialog box.

  2. Right-click on the new project title in the Solution Explorer and select Set As Startup Project from the pop-up menu, as shown in Figure 5-6. Visual Studio .NET makes the project name bold, indicating it is the startup project.

    Figure 5-6. Make the test project the startup project for the solution


  3. Add code to the test project's Main procedure to test the .NET component.

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 Excel


Now, 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.



    Excel 2003 Programming. A Developer's Notebook
    Excel 2003 Programming: A Developers Notebook (Developers Notebook)
    ISBN: 0596007671
    EAN: 2147483647
    Year: 2004
    Pages: 133
    Authors: Jeff Webb

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