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.'
Related
I'm going through someone else's code where they have a using OpenDataAnswer.DataSchemas; statement. I have a working version of their code but need to copy a file to another project and I cannot figure out how to resolve this using statement. I cannot find a dll reference of the same name and a search of the entire solution does not show another namespace by that name. Is there any way I can see where this import is coming from?
If you are using Visual Studio you can put your cursor on the last identifier in the namespace (in the working version of your code) and hit F12. This will display all the assemblies that use the selected namespace:
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.
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)
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.
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!