Why has my solution stopped recognising Using Directives? - c#

I have a solution, which I was working on yesterday without issue. Today I have opened the same solution and VS 2008 has underlined certain using directives in red. Mousing over the underlined directives brings up the tooltip: "Am I missing an assembly reference?" I am not, as I tried to add the references in and it does nothing to solve the problem.
Weirder still the project compiles and runs without a problem so it's only VS2008 that's having a problem with the project. I'd like to get all my objects back to a state where Intellisense recognises them, trying to maintain the code when VS doesn't recognise objects like DataTable, RegEx and TableCell is very frustrating.
The using directives at the head of my page are as follows, the bold ones are no longer recognised after the System namespace reference, the others work fine:
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
I don't really understand what's changed overnight to break the project. Any suggestions?

Some things you could try:
Delete the .suo and .csproj.user files in the solution and project directories, respectively. (The .suo file has the hidden attribute set.)
Create a new, blank project which doesn’t exhibit the problem you are experiencing, and then compare (using a file-compare tool) the new .csproj file with the .csproj that exhibits the problem. Remove things that appear to be redundant.

One thing that immediately jumps out is that all of the using directives that are not working come from assemblies other than the core .NET assembly. I would double-check that the references to these assemblies are still valid within your project file.

As some comments under my question recommended (and completely against the instincts of common sense) cleaning the project, rebuilding it and then restarting VS2008 did the job... Who can say why?
The only difference in my PCs start up this morning was that the company approved security solution McAfee System Hog (or whatever it's called) decided to soak up most of my system's resources downloading and installing some sort of update. During this period my PC was super slow to respond to anything I told it to do. Maybe the McAfee install interfered with the usual VS startup sequence.
Anyway, works now but I shall bear in mind other suggested solutions in case it ever happens again. Thanks!

Related

How to configure namespace adjustments by ReSharper?

I'm having a problem with ReSharper refactoring I just can't find a solution to.
Whenever I'm trying to adjust a name space through refactoring, when it says "Move to 'correct.namespace' namespace", ReSharper forcefully removes unused "using" statements from all the files it fixes the namespace in and it also opens all the files.
My question is, how to prevent ReSharper from opening all the files it modifies and also how to stop it from removing unused usings?
I don't want it to open 20+ files, neither do I want it to remove the standard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Please help.
Add them to the Namespace Imports part of the Resharper Options to preserve the namespaces.
I'm not sure if its possible to prevent it from opening all of the files since it depends on so much of the VS infrastructure. Plus it would not be possible to undo without opening the files.

Browser detection in C# (.Net)

I'm in C# land today. I'm trying to write a function which accepts a user agent string and returns an object that gives me at least the browser name and version. So I tried this answer, but apparently I don't have access to HttpBrowserCapabilities. It tells me the type or namespace name could not be found (yes, even if I add using System.Web, it still doesn't show up, or when I type using System.Web. it doesn't pop up, so it's obviously not there).
I'm using .net 3.5, but the documentation for that class shows it existed even in 3.5, so I'm not sure what's going on. I have access to the browscap files - ini or xml. Am I going to have to write this from scratch?
Edit: I've fixed the reference problem. But Chrome is being reported as AppleMAC-Safari 5.0. I'm wondering if I'm going to need a completely different approach. PHP figures it out with the same ini file fine.
Adding a using block does not automatically import the DLL. All a using does is allow you to not write:
System.Web.HttpClient //Or whatever
All over the place, and use HttpClient instead.
You need to add a reference to System.Web to the project before any of its classes will be available to you.
Did you have a using System.Web; statement in your source file?
Here's a tip: if you're using Visual Studio, and you have a reference to the System.Web.dll in your project, if you type the name of a type and press Ctrl-. it will give you a popup menu to add the namespace reference to your source file.
Do you see it in the ObjectBrowser (assuming you are using Visual Studio)? I found the namespace this morning (granted I'm on 4.5 - but documentation shows it has been around since 3.5 and earlier)

Visual Studio failing to detect existing objects, even from within the same file. Project still builds

I just reopened a project I haven't touched in a few months, and my copy of VS2012 is doing some strange things. I've seen it do these things before, but usually it works itself out after a Clean or after restarting VS.
I have a Common project, which is referenced by at least 2 other projects in the solution. I have a cs file that contains 2 enums, and a class. I reference those enums within the class, but VS gives me the little red underline that indicates a syntax error.
"The type or namespace name "SourceTypes" could not be found (are you missing a using directive or assembly reference?)"
The enum is literally located above this class. It is public. Also, if I do a build, it will build correctly. But the error comes back after the build finishes. Sometimes it doesn't build properly, and I have to build this project first without doing a build all. I have no idea why this reference has started doing this suddenly.
I also installed CodeMaid and AllMargins recently, but I don't think either of those could cause build errors.
Also, the common library is using Framework 3.5, because it is referenced by two other projects, one of which is SharePoint, and therefore 3.5, and the other of which is a WPF app, running 4.0 (not Client Profile)
Any ideas?
Update: Clearly the problem is that it doesn't recognize the namespace. In the using statement in multiple files, the namespace has the red line under it. Maybe I've got some kind of namespace conflict? But if I did, I would expect it to fail when I do an actual build, which it does not.
Solution: Feri got the right answer. By unloading the Common project, and then reloading it, the problem went away. All references to the namespace were corrected, and everything works smoothly again.
Solution: Feri got the right answer (but opted not to submit his solution as an answer). By unloading the Common project, and then reloading it, the problem went away. All references to the namespace were corrected, and everything works smoothly again. However, the problem seems to come back whenever I close/reopen VS. Unloading/reloading the project fixes it each time though.

Using Directives Sorted in Wrong Order

I'm using the Power Commands extension with Visual Studio 2012. I have the option checked to remove and sort usings on save. The problem is that the System.Xxx directives are being sorted last, and that's causing a style analysis error:
SA1208: System using directives must be placed before all other using directives.
Before save:
using System;
using System.Diagnostics.CodeAnalysis;
using Foo;
After save:
using Foo;
using System;
using System.Diagnostics.CodeAnalysis;
This worked correctly (System.Xxx first) with VS 2010. Anyone know how to correct this?
Note: Even if it didn't cause an SA error, I'd still prefer the system directives to be first.
Goto the "Quick Launch" (Ctrl+Q) and type "using" and press Enter.
Then change the following setting:
It's an annoying default setting, I have no idea why Microsoft chose that, it goes against all previous standards that I've ever seen.
EDIT:
Thanks to Oskar we have a reason:
The reason for the change in default behavior is due to the fact that
Windows App Store applications prefer to have 'Windows.' at the top
of the file rather than 'System.'

VS 2010 keep stating that "This using directive is not required"

The following using statements in a regular class keeps screaming at me that "The using directive is not required".
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
Yet if I try to use the DateTime data type intellisense will not pick it up and the compiler complains when I try to build.
You could organize them:
Then when you want to use a new type in your code which is not in your usings declarations simply place the cursor over it and Ctrl+Alt+F10+Enter
This being said, organizing and removing unused usings is not compulsory. This has already been discussed here. It has strictly no difference at runtime nor at the emitted assembly. You should do it only if you are anal about how your source code looks like and have things properly organized. I do it all the time.
It ended up being a misunderstanding of how VS 2010 works. The IDE tells you that you're not using a library until you actually type the code that requires the library.
I thought the errors were being generated after I build the solution. The "organize usings" menu item in the context menu helps clean this issue up quick. Thanks for all your feedback.
Vs is telling you that you have added some libraries that are no required. remove the not used libraries and make a clean solution.
I think you are getting this error for using System; statement. Try removing that statement.

Categories