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

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.

Related

Insert simplified Using Statement by Intellisense in VS 2017

The AutoComplete Function in VS2017 suggests me fully qualified using statements.
I have two projects with following (simplified) structure:
Company.Contracts
IMyExample.cs
Company.Core
MyExample.cs
Now when I use IMyExample in Class MyExample, VS2017 suggests me a using statement like
using Company.Contracts
But I think that there was a time when VS2017 suggested me:
using Contracts
which is sufficient as the projects share the same main namespace.
How can I configure VS2017 so that it prefers simplified instead of fully qualified namespaces? In fact this is the opposite of StyleCop Rule SA1135.
Hint: I was using VS2019 before but switchted back to VS2017 because the test licence ended and I'm pretty sure, that I didn't have to correct my using statements. Maybe in VS2019 this is possible?
Maybe in VS2019 this is possible?
Sorry but the answer could be negative, I test it in VS2019 release 16.3.4 and confirm this behavior is not supported for now.
In your situation, instead of using full qualified names, you can also use the format like Contracts.ClassName.
And for the reason why full qualified namespace is more preferred in this situation, assuming your current project Company.Core references one assembly whose root namespace is also named Contracts, now if VS do what you suggested, add the using Contracts when you use functions from Company.Contracts project, the intellisense would be confused about this. See:
In that situation you suggested, VS intellisense may get confused about what the using Contracts really mean, another assembly whose root namespace is Contracts or Company.Contracts project? So I think this could be one possible reason why it suggests full qualified namespace.
And if you do need one option which supports this behavior, I suggest you can send a feature request here.The team would consider about it if this request gets enough votes.
Hope my answer makes some help:)

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.

System.Windows.Threading not working

I am trying to use using System.Windows.Threading; but it gives me
The type or namespace name 'Threading' does not exist in the namespace 'System.Windows`.
I have the latest version of .NET.
It's in WindowsBase.dll, after add the reference, it works!
(if you don't know how to add reference, see this tutorial.)
Please make sure you've added a reference to WindowsBase.dll to your project and then you should be good to go. have a look at - https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx
If you're still having trouble please attach your .csproj file.
You have to add a reference to the assembly containing the class you want to use.
Additionally, you probably don't want to use WPF classes (from System.Windows.Threading) if you did not create the project with the WPF application wizard.
Edit: In Windows Forms applications, the obvious choice for a timer is System.Windows.Forms.Timer, and in some cases System.Threading.Timer.
It's using System.Threading; not using System.Windows.Threading. Don't know if you used that in your code and just made a mistake on the question, but there it is.

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.'

Why has my solution stopped recognising Using Directives?

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!

Categories