While experimenting for my series of articles on building a development environment using Visual Studio 2003, I came across the following:
Let’s say you have a software component which implements a particular concern of your application, and this component has a class named MyClass. Thus, in Visual Studio we have a project of type “Class Library” which is contained in a solution and inside this project we have a file MyClass.cs which contains the code for our class. You compile a debug version of this library which gives you a DLL and a PDB file (program debug database).
Next you have your application which also has a class MyClass, but in a different namespace, however also in a file called MyClass.cs. Your application uses the above library, but it references the library DLL itself and NOT the Visual Studio project file. For this you copy the DLL and corresponding pdb file to the debug folder of your application. Also, the source code of the library is no longer available at the location on your harddrive where it was when the library dll got build.
Now, when you start debugging your application and want to step into the code of the MyClass of your library, Visual Studio jumps around in the MyClass.cs file of your application.
What is happening
It is clear that Visual Studio is mixing up the sourcefiles of your library with the ones of your application:
Because the source code is no longer available at the original location, Visual Studio can not go there to look for it. But because the sourcefile in your application has the same name as the one in your library Visual Studio assumes this is the source file of your library and starts jumping around in your applications file as if it where your libraries file.
A possible solutions
The solution is simple, keep the names of your sourcefiles unique. The easiest way to do this is simply by prepending the namespace to the name of your files.
This code like this:
public class MyClass
// Rest of class ommited
ends up in a file named ClassLibrary.MyClass.cs
I’ve tried the same in Visual Studio 2005 and found no problem there. So you could switch to this IDE. But this will not be possible to everyone of course.