65.9K
CodeProject is changing. Read more.
Home

Getting round Visual Studio when it decides a class isn't the same as itself...

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.92/5 (3 votes)

Oct 31, 2013

CPOL

2 min read

viewsIcon

7830

Sometimes (far too often, I find) you will try to open a form in the Visual Studio designer, and it won't open it because "the type X declared in Project X is not the same as the type X declared in Project X". This is annoying, as well as being blatantly untrue. Stop laughing at the back there!

Introduction

Visual Studio keeps temporary intermediate files which it doesn't handle very well sometimes.

And every now and then if you have a Solution with multiple projects - as is recommended by good practices - VS ends up with two copies of intermediate files for the same project - and it ends up complaining about designer generated code like this:

protected MyWalletBL.Branch Branch;
...
this.selectFavourites.Branch = 
  ((MyWalletBL.Branch)(resources.GetObject("selectFavourites.Branch")));

Saying that the class is different on the two sides of the equals sign - which is blatantly untrue!

What has happened is that somehow, VS has got confused and is using two different intermediate files with slightly different class signatures (even if you haven't changed the class) and doesn't know what to do.

So when you try to open the form, it throws up it's electronic hands, and collapses.

Fixing it!

Stop right where you are. Do not do anything else: VS is in a fragile state right now, and could easily start to corrupt stuff.

It's actually pretty simple to fix:

  1. Remove all inter-project references. 
  2. Clean solution. (I don't think this does anything useful here, but it can't hurt) This does not get rid of the intermediate files. 
  3. Close VS. Wait until it is completely gone, the disk has stopped rattling, etc.
  4. Delete the intermediate files. Where are they?  Here on my PC:
  5. C:\Users\griff\AppData\Local\Microsoft\VisualStudio\10.0\ProjectAssemblies

    Which means they will be under your user name - but be aware than AppData is a hidden folder, so you will probably be best creating a shortcut to this location using your username. I find that once this problem crops up, it will be back, and back, and back...

    Delete everything in that folder. All the files (shouldn't be any). All the folders. If you can't delete a folder, then VS is still running. Find it. Kill it. Try again. 

  6. Restart VS
  7. Re-instate the references, and rebuild the solution. 

Done! 

Points of Interest 

There are times when I hate VS... 

History

  • Original version.