Friday, May 18, 2007

MissingManifestResourceException

I came through this strange exception today while refactoring a windows forms application in VS 2005. This was a VB.NET application.  I had to move a usercontrol between projects and doing so I changed the namespace. Suddenly I was getting a MissingManifestResourceException when the control is loaded. The strange thing is there were some other user controls in the same namespace and they worked fine.

Then I inspected the generated assemblies using reflector and discovered that the failing controls resource didn't have any namespace.

After a bit of googling I found this blog entry http://jameswho.blogspot.com/2004/06/missingmanifestresourceexception.html#links this entry suggested changing the default namespace. But I always keep it blank and specify the namespace in each file. Scrolling through the comments there were some suggestions to remove any class or enum declarations that came before the control class. This was exactly the problem I had, there were a couple of enum declarations and a delegate before the control class; after moving them to the bottom of the file it worked fine.

I think this is a bug in visual studio that you have to watch out. It is always a good practice to have only one type in a given source file.