Insert simplified Using Statement by Intellisense in VS 2017 - c#

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:)

Related

How to know what assembly to add C#

I've ran into this issue a couple times and I'm wondering if anyone has a better solution than trial and error or searching stack overflow.
Lets say we are using some .net class Foo
Foo resides in the Bar.Baz namespace
The following statement
using Bar.Baz;
is not sufficient to compile the program, we are missing an assembly reference. So add a reference to System.Bar.Baz It still doesn't work so after searching the internet I find that I actually have to add a reference to Some.Other.dll and now it compiles.
My question is how do I know what namespace maps to what reference when the usual one doesn't work?
Most recent problem was
The type or namespace name 'DbContext' could not be found Instead of adding a reference to System.Data.Entity I had to install through Nuget.
If it is a .NET framework function, you can just search it on MSDN, and it will tell you in which assembly the class/function exists.
You can also use ReSharper which is a very nice plugin to Visual Studio, and it can help you add assemblies automatically.
If you're using Visual Studio 2013 or higher, one easy way to discover which namespace a class belongs to is using the Peek definition feature. You can easily find it in the right-click context menu.
In the screen below, I used it with KeyValuePair:
Also, take a look at the documentation.

.NET 4.5 namespace 'Standard'

In PresentationFramework from .NET 4.5 there is a namespace called Standard. Look here for more info: What is the namespace 'Standard'?
The problem is that in my C++/CLI project I am using an unmanaged library, which also defines a class called Standard. So I get the following compiler error:
error C2869: 'Standard' : has already been defined to be a namespace
I cannot remove the reference to PresentationFramework, and I cannot stop using the said library. Is there anything I can do? Like un-importing the namespace?
P.S. I am using VisualStudio 2012. I think that an upgrade to 2013 might help, but that will require the whole team to move to it.
That namespace was added to PresentationFramework by .NET 4.5, I believe, and I don't think changing to Visual Studio 2013 will help you. Everything in that namespace is defined as internal, and it mostly consists of Enums and Structs used with Windows SDK functions called by PresentationFramework.
Unfortunately, I have no idea what to do about your problem. Perhaps you can convince whoever supplies the third-party library to change their namespace. The fact that Microsoft is now using it would be a good reason for them to do so. Whoever these people are that are creating namespaces with a simple, generic name such as "Standard" need to have their heads examined.

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)

SDKs in Visual Studio 2012

I am brand new to Visual Studio. I have been coding in Java for many years but have taken on a project which requires me to use c# and visual studio 2012.
What I need to know is how to utilize a different SDK. I want to use something called Honeywell SDK instead of Visual Studios inherent SDK but I cannot find out where to change this setting. If anyone has an answer that would be greatly appreciated!
as a Java developer you are probably used to imports and presumably understand how to use the import statement to import the classes in a namespace.
In C#, the first thing you must do is add a reference to the library containing the methods you require - this is normally done by right clicking your project in Solution Explorer, clicking add reference, and then selecting browse to browse to the location what is normally a DLL containing the library methods in question.
Once you have added a reference to your project, you can access the classes in the library either using a fully qualified name, e.g. to access the Thread class in .NET's System.Threading namespace for example, fully qualified use would be as follows:
System.Threading.Thread thread = new Thread();
Alternatively, you can put a using directive at the top of each file where you intend to use the client to avoid the need for the fully qualified name. For example:
using System.Threading;
Then in code, you can simply use the shortened version of the class name by itself:
Thread thread = new Thread();
As you can see, the using directive is effectively C#'s equivalent of Java's import directive. Note that to import all classes in a namespace you do not need the .* wild card at the end of the using directive as you do an equivalent Java import statement.
In practice, you may need to refer to the documentation you have to confirm what namespaces they use, and what files you need to add references to to use their libraries as this detail will be vendor specific. For more detail and a more thorough explanation of the using directive then the MSDN documentation is likely to be the most helpful source:
http://msdn.microsoft.com/en-gb/library/sf0df423%28v=vs.80%29.aspx
and:
http://msdn.microsoft.com/en-gb/library/z2kcy19k%28v=vs.80%29.aspx
There is no inherent SDK per-se in a .NET project, though normally references to the .NET framework and default using directives will be added. You will probably find these useful as they contain core functionality and the references normally added by default in a new project will provide you access to things such as collections and so forth.
One final note is that C# has a using statement, as well as the using directive, so if searching for additional information on the directive, be careful not to confuse it for the using statement.

Ambiguous references in VS 2010

Trying to upgrade a solution from 2008 to 2010. And I suddenly get a lot of ambiguous reference errors compiling in VS 2010.
It works fine in 2008. Is VS 2010 more strict regarding the using directives?
I had a similar issue.
I dont think it is stricter, but more a coincidences of the newer framework now having the same class name I was using in the dlls referenced, either things were moved or there was some new development to existing dlls.
It took some time to fix the entire project, but the ways around it I found were:
To use either define the full location of the classes
or
define an alias:
using CompanyMagic = Core.Company.Magic;
have you references to two different dll-versions of the same assembly in your solution?
For example are you referencing "System.dll from dotnet 2" plus "System.dll from dotnet 4"?
There is a quite similar post on StackOverFlow : Ambiguous reference error in VS2010
From this article, from Richard :
There may be slight changes in lookup rules about how different cases (e.g. identifier in current namespace takes precedence (as in class Program above).
There are two options to disambiguate:
Use fully qualified identifiers, in the above, either System.Action or ConsoleApplication1.Action.
Use a namespace alias to fully qualify the identifier without so much typing:
using MyAction = MyNamespace.Action;

Categories