Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 suddenly can't see namespace?

My C# WinForms solution has two projects. A DLL which is the main project I'm working on, and an executable WinForms I call "Sandbox" so that I can compile/run/debug the DLL easily in one go.

I'm working in .Net 4.0 for both projects.

Everything was working fine until I added some seemingly innocent code, and a reference to System.Web in the DLL. Now my Sandbox project can't see the namespace of the DLL project. I didn't change anything which I believe should have affected this.

If I delete the project reference to the DLL from the Sandbox references and re-add it, then the red underlines all disappear and the colour coding comes back for all my classes etc; but as as soon as I try to build the solution, the whole thing falls apart again.

When I right-click the DLL project in the Sandbox's references and view in object browser, I can see the namespace and all the stuff in there.

I have a feeling this might be some sort of bug?

Is this some sort of VS2010 bug? I had this same issue a few months ago and I could only fix it at the time by making a whole new project and re-importing my files. This time, however, I have a bajillion files and will only do that as a last resort!

Edit: After panickedly going through and undoing all my changes, trying to find what caused the problems, it seems to be this line:

string url = "http://maps.google.com?q=" + HttpUtility.UrlEncode(address); 

If I comment out this line, then I get no namespace errors and the project builds fine. I can't see anything wrong with this line though.

like image 915
Ozzah Avatar asked Feb 02 '11 22:02

Ozzah


People also ask

How do you fix the type or namespace name could not be found?

Solution 1If it has an "include" option, click that, and it should fix it. If it doesn't, you need to find out where that class is, and add a reference to uit, either via the project in the same solution, or via the DLL, and then add the appropriate using line to the top of all files that reference it.

How to fix missing references Visual Studio?

To fix a broken project reference by correcting the reference path. In Solution Explorer, right-click your project node, and then select Properties. The Project Designer appears. If you're using Visual Basic, select the References page, and then click the Reference Paths button.

How do I add a namespace in Visual Studio?

To add an imported namespaceIn Solution Explorer, double-click the My Project node for the project. In the Project Designer, click the References tab. In the Imported Namespaces list, select the check box for the namespace that you wish to add. In order to be imported, the namespace must be in a referenced component.

How do I automatically add a namespace in Visual Studio?

To do it, you may either manually type the Using Namespace or you just right click on the class name and select Resolve > Namespace. But using “Ctrl+.” you can automatically add the namespace in your code.


2 Answers

I'm ready to declare this a bug in VS2010, this has bitten way too many programmers already. The fix is easy: Project + Properties, Application tab, change Target Framework to ".NET Framework 4" instead of the Client Profile that is selected by default.

System.Web is not included in the client profile. Having this option in the first place is quite silly, the client profile is only 15% smaller than the full version of .NET 4.0. Having it selected by default is even sillier. But I digress.

UPDATE: mercifully this all got fixed in VS2012. Which no longer makes the client profile the default for a new project. And the client profile got retired completely in .NET 4.5, good riddance.

like image 64
Hans Passant Avatar answered Oct 03 '22 23:10

Hans Passant


Check to make sure that both projects are using the non-client profile for their target framework (go to each project's properties to do this).

like image 40
Mark Avenius Avatar answered Oct 04 '22 00:10

Mark Avenius