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 startup project. To add a test project to the NetForExcel sample, follow these steps:

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

  2. Select the Console Application template from the Visual Basic project types, give the project a descriptive name, and click OK. Visual Studio .NET creates a folder and template files for the new Windows console application.

  3. 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 25-6. Visual Studio .NET makes the project name bold, indicating it is the startup project.

  4. 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 ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.