C# not detecting and instantiating classes - c#

I started writing a new C# app. I have created a couple of classes that i would like to declare and instantiate in a Form. The thing is, when i write it, the intellisense does not detect it. I will provide a screen shot, as I don't know how to better explain.

DataHelper is defined under the BuildID namespace.
You have 2 options:
The first is to declare it like this:
private BuildID.DataHelper _dataHelper //...;
The second is to add a using BuildID at the top of the page, you can either write it manually or have your cursor on the DataHelper object which is unrecognized (at the moment), press
Ctrl + . and choose the option that the Visual Studio offers you, to add the using statement automatically.

I Think to instantiate it you wanna do
"Private DataHelper data = new DataHelper();

Related

VS Code: Extract interface in C#

I'm using VS Code and I can't figure out if it's possible to take a class and extract an interface from it. When I google how to do this, I only find extensions for TypeScript, but I want to do this in C#.
Can VS Code extract interfaces? Knowing the shortcut would be nice for others, but I'm using a different keymap, so I'd like to know how to do this from the menu.
You may try this,
Move your cursor on the class_name that you want to extract interface. Then Ctrl + . (period)
Then choose "Extract Interface" this will generate a code above the class that you want to be extracted.
Then move your cursor the interface class_name Ctrl + . (period) again, then choose "Move type to the" auto-generated class name then that's it.
Hope this will help you guys and for future reference.
Not really sure if this feature was already there or got implemented afterwards, but I found this:
First, extract the interface as usual
Then you get to do this on your methods:
Note that it doesn't add necessary usings but uses namespaces instead. You can do the clean up as you like.

Quickest way to declare a field and constructor-inject it

Using Visual Studio 2015 Enterprise with Resharper Ultimate.
I find myself often needing to declare a new field, and then have it assigned by a constructor argument. As far as I know, the quickest way is to:
Navigate up to the top of the file where the field declarations are
Declare the field:
private readonly NameOfType _nameOfInstance;
alt-enter on the field name, "Initialize field from constructor"
navigate back to where I needed the object and continue
This isn't too onerous, I just find that I do it so many times a day, I'm left wondering whether there's a faster way. Is there a Resharper / VS function / plugin where I can press a key, specify a type, and have it do all of those steps for me? The _nameOfInstance is nearly always just _nameOfType, and it's always private readonly and constructor-injected, so it would be good to reduce this bit of busy-work from my day.
With ReSharper, you can place the cursor in (or highlight) the parameter in the constructor and use CTRL+R, CTRL+F to create a field from it.
You can type an unused symbol name in your code and hit Alt+Enter on it to automatically create a field (or even better, type the new field name, followed by .field, which will act like a template/snippet and automatically create a field with that name at the top of the class). You can then Alt+Enter on the new field and select "initialise from constructor" to wire it up in the constructor. This will move you out of the current method, though. ReSharper doesn't offer a way around that, but you can use the "Navigate back" command (Ctrl+Minus) to jump back to the method you were working on.

How can I perform "Go To Definition" programmatically in Visual Studio?

Given a string that represents a specific class/field/property (eg MyNameSpace.MyClass or System.String.Length), how can I programmatically cause Visual Studio to go to that class/field/property (ie, make Visual Studio do the same thing that would happen if I was to type in the reference in the code editor and then hit F12)?
You probably need to do the following.
Get the global IVsObjectManager2 interface (implemented by the SVsObjectManager object)
Call IVsObjectManager2.FindLibrary to get the C# library, and cast the result to IVsSimpleLibrary2.
Call IVsSimpleLibrary2.GetList2 with the correct VSOBSEARCHCRITERIA2 in order to locate the symbol within the projects for your solution.
If the resulting IVsSimpleObjectList2 has GetItemCount()==1, and CanGoToSource with VSOBJGOTOSRCTYPE.GS_DEFINITION returns pfOK==true, use the GoToSource method to jump to the source.
Otherwise, rather than jumping to the source, simply display the possible options to the user. You will be able to use the IVsFindSymbol interface (implemented by the SVsObjectSearch object) to for this.

Two identical object names; how to make VS ignore one of them?

I have basic User model class that I am using but the problem is that in ASP.NET MVC there is already some property named User that is getting HTTP authentication information.
Can I somehow make Visual Studio 2012 ignore the other one and use mine? It would be pretty unpractical to write Website.Models.Users.User every time I want to access it.
Here is a picture for you to understand my problem better.
You can alias your Namespace or Class, or Microsoft's Namespace or Class:
Replace:
using Website.Models.Users;
With:
using myUsers = Website.Models.Users;
or
using myUser = Website.Models.Users.User;
Then use it as: if(myUser. or if(myUsers.User. in your code
Alternatively, you could just rename your classes so there are no collisions.
While Tom Studee had a good idea, there's an even better one.
Not only can you alias namespaces, you can alias classes!
Use this;
using HTTPUser = Website.Models.Users.User;
and then when you want to access that use use HTTPUser for that type of user and just User for the normal one.

C#/.NET How do I find the containing namespace from a class name

I often find myself remembering the name of a class that I want to use, but not remembering the containing namespace.
Apart from searching the web, i wonder if a good method exists for looking this up.
I think if you press ALT, SHIFT and F10 in Visual Studio - intellisense will drop down an option for you to add the name space of the class you have just typed.
CTRL + '.' will bring up a menu where you can either add a 'using' or fully qualify the class.
You can always hang a big poster on your cube wall like me.
3.5 NameSpace
If you know the name of a class in .Net but have no idea what namespace it is in, it can be hard finding it, especially if you dont have a reference/using to the assembly containing it.
This is where the Object Browser (Ctrl+W,J) comes in handy.
Open it up, type in the name, it will give you all matches, either within your project/solution, or all of the .Net framework.
Edit:
As S.C. Madsen's comment points out, this also helps if you only remember PART of a class name, also if you only remember a method name but not the class.
Use the search function in .NET Reflector by Red Gate Software.
I generally use the offline MSDN reader, with the left panel set to the Index tab.
Another option in Visual Studio is to type the name of the type as if you were declaring a variable, and then see what it suggests. If the name goes to a light blue colour (by default) then it's in one of the namespaces you're already importing - just hover over it to find out which. Otherwise, see what namespaces it offers to add using directives for.
You can right click and select "Go To Definition" in VS and this will either load the class definiftion in your solution or it will show a metadata view of the class definition using reflection. Either of those should have the namespace defined near the top of the page.
If you need to add the namespace with a using decliration right click the unresolved class and mouse over to resolve. It will show you a list of namespaces that contain that class and selecting one will generate the using statement.
Two ways that work in Visual Studio 2013:
Right-click and select "Resolve".
Hover over the class and a 'Options to help bind the selected item' box will appear (same as Ctrl + '.' or Alt+Shift+F10)
Select the namespace and it will insert it for you.

Categories

Resources