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.

1 comment:

Hasith Yaggahavita said...

Shitty .NET. I also faced a crazy runtime error with .NET which no one would expect with a compiler. In my code I had a IF block with a combined condition (There was two complex conditions connected with OR). VB.NET Runtime was evaluating the combined condition faultily. When I moved the two complex conditions to separate boolean variables then the IF condition started behaving correctly. That was pretty weired and debugging the problem took few days as no one would expect a commercial runtime to be that foolish.

Hasith