Click here to Skip to main content
15,883,799 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.92/5 (3 votes)
31 Oct 2013CPOL2 min read 7.5K   2  
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:

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
-- There are no messages in this forum --