c# Where can I lookup Microsoft Namespace names? - c#

I'm using a MS namespace but Visual Studio is telling me I don't have a reference to it. Is there a place you can go to and lookup namespaces?
Thanks

If you mean "to find which dll I need (per-type)": MSDN?
For example, CLSID
Namespace: Microsoft.Aspnet.Snapin
Assembly: AspNetMMCExt (in AspNetMMCExt.dll)

You can normally find the MSDN page about a specific namespace by going to http://msdn.microsoft.com/namespace. So, for example, to find out about System.Web you could go to...
http://msdn.microsoft.com/system.web
That in itself doesn't help you. You'll need to click through from there to the specific types you're using, and it'll tell you (near the top) the name of the DLL that implements the type.
Remember that a namespace can contain types that are defined in more than one DLL.

See http://msdn.microsoft.com/en-us/library/wkze6zky(VS.80).aspx for how to add a reference

You will also need the assembly.
For Microsoft and System namespaces the easiest way is http://msdn.microsoft.com/library or, if MSDN installed locally, its index.

If you want to know which assembly a certain class is located in you can simply check the documentation (it's noted on the class overview page of the class). Note that one namespace might very well be spread out over more than one assembly.

You need to first add reference to the DLL before using it in your code with the 'using' keyword.
Right click the project > add reference > in the .Net tab select the component and click ok. Then build your code.

You can't find the DLL for a specified namespace in all cases because multiple types belonging to the same assembly may reside in different assemblies.
The fastest way to get there would be to google to the MSDN page for the specific type (class) you are using. Let's say XDocument.. I put `msdn xdocument class' into google. First result is the page I need. Click!
Under the class name you'd see a section like this
Namespace: System.Xml.Linq
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
This shows you the namespace that the type belongs to (for which you may need to add a using in your code)
and the DLL you need to 'Add Reference' to.

Related

C# namespaces: Calling a method in two identical namespaces with same name

I'm trying to understand C# namespaces. Suppose two namespaces exist with the same name and they have identical contents, i.e. all classes and methods also have identical names. How can the compiler distinguish between the two if I try to call a method, e.g. MyMethod in class MyClass in namespace MyNamespace? It's not likely a real-world scenario, but I find the information useful for understanding purposes.
Second question: When I dot on a namespace, e.g. System, I get a set of related namespaces like System.Configuration etc., but when I go to Microsoft's documentation I cannot find any namespace named Configuration contained in System namespace. Why is that? I can of course find the namespace if I look for System.Configuration, but I don't understand why the System.Configuration namespace is not nested inside the System namespace.
If you have two types that have the same name and same number of type parameters, and exist in the same namespace, then the assemblies that they are in must be different.
If your project references the assembly that one type is in, but not the other, then you will obviously only be able to refer to the type that is from the assembly that you referenced. If your project references both assemblies, then there will be a compiler error, like this. To fix it, you can use an extern alias.
Not being able to find System.Configuration in this page is merely due to how Microsoft organises their documentation. They have decided to list all the namespaces out here, rather than display a hierarchy. Note that System.Configuration is part of the .NET Platform Extensions, not plain old .NET, so you have to choose that in the dropdown.

WPF - src type not found

I tried to use this link: http://msdn.microsoft.com/en-us/library/ff407130.aspx . But whenever I debug it for some reason it throws an error :
The type 'src:MyVirtualizingStackPanel' was not found. Verify that you
are not missing an assembly reference and that all referenced
assemblies have been built.
And another error:
'src' is an undeclared prefix. Line 13, position 22.' XML is not valid
I copied the class and methods into the MainWindow class and the XAML.
Am I missing anything here?
At the end of the day, you are mistaken about your XML Namespace. If you type your error minus the name into a search engine, you will see something:
The type was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
All of the results will point to the fact that you have not provided the correct XML Namespace for your project. Now we don't have your project in front of us... only you do, so only you can work out what your Namespace problem is. You mentioned something in a comment that made me suspicious.
You said The name-space is set automatically, but when I asked you to tell me exactly what that meant, you didn't, so I still don't know what you meant. Either way, it doesn't matter, but that could be a cause of your problem. Here are some other possible causes:
Maybe your MyVirtualizingStackPanel class is defined in a different class/project to where you specified.
Maybe you need to include the assembly=XXX part of the XML Namespace.
Maybe you have not included the CLR Namespace in your MyVirtualizingStackPanel class.
Either way, this is definitely an XML Namespace problem, so just keep looking/experimenting and you'll find the problem eventually. You can find out exactly how to add an XML Namespace from the XAML Namespaces and Namespace Mapping for WPF XAML page on MSDN.
Yes, you need to specify the namespace where the MyVirtualizingStackPanel class comes from.
In the beginning of your XAML, add:
xmlns:src="clr-namespace:XXX"
where XXX is the namespace.
See MSDN.
You have to import in the xaml the namespace srcto use this
On the top of the page you do:
xmlns:src="yourProjectPath"
than you can use the MyVirtualizingStackPane class.
add the XMLNS in the top,
Here look at the example
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Add a reference to 'PresentationFramework' - you will find it in:
Add reference... -> Assemblies -> Framework

why we give reference to the other projects?

When looking at a solution with multiple projects:
1) Why do we add a reference to the other project? Can't we just use inheritance?
2) After we add the reference by using Visual Studio, why do we have to add the project to the namespace system? For example: using myReferenceProject; I thought that the IDE would do that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myReferenceProject;
using System.Data.SqlClient;
using System.Data;
1) why we give reference to the other project? cant we just use inheritance???
They're two completely different concepts.
Adding a reference to another assembly basically says, "I want to be able to use some of this code. Please make the compiler aware that this code exists, and optionally copy it into the output directory so that it's present at execution time too."
How would you expect to use inheritance to create a class derived from some type if the compiler has no knowledge of that type?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system???
Because assemblies and namespaces are different concepts too. A using directive simply says to the compiler "When I refer to type Foo within my code, look in these namespaces to try to resolve it." That type could come from any of the assemblies you've referenced.
It's really important that you understand the difference between assemblies and namespaces. Even though they're often named similarly (Foo.Bar.dll often provides types in the namespace Foo.Bar) they're conceptually different.
The project is a self sufficent compilable unit, that has to compile into the valid assembly file. That's why you need esplicitly specifiy which referencies every project needs in order to be able to compile separately from others.
There is no any inheritance concept on projects level.
1) why we give reference to the other project? cant we just use inheritance?
This question makes no sense. What does inheritance have to do with project references. Can you elaborate?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system?
Because there's an inherent difference between an assembly referencing another assembly (which is what happens when you add a reference to the project) and the language knowing where to find a class which is what happens when you use the using directive in a file.
For example, suppose you create a class in your project called TextBox. Then in another file you want to use that class. How would the language know whether you are referring to your custom TextBox class or another one in a referenced assembly? The answer to that question is namespaces. By fully-qualifying the class name with its namespaces, you tell the compiler which class you're using.
The using directive is a way to specifying the namespace once per file instead of every time you use the class (or other classes in that namespace). So if you need to reference your TextBox class multiple times within a single file, you wouldn't want to have to write this every time:
MyCodebase.MyAssembly.MyNamespace.MyOtherNamespace.SomethingElse.TextBox
Instead, you include a using directive of the entire namespace, so you only have to write this:
TextBox

How do I know what reference to include to import a specific .NET namespace?

This is in reference to a problem I had which I was able to find the solution to here on Stack Overflow.
This wasn't my question, but it was the same exact problem I had. How do I know that I need to import a specific DLL file to use a specific namespace? I saw no reference to this on either the MSDN page or the Object Explorer, and even ReSharper didn't pick up on it. Is this referenced anywhere?
From http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx:
DataContractJsonSerializer Class
Serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects. This class cannot be inherited.
Namespace: System.Runtime.Serialization.Json
Assembly: System.ServiceModel.Web (in System.ServiceModel.Web.dll)
Good question. I generally do a quick search for the class on Google, then start typing for the assembly based on the beginning of the namespace it's in.
When you look up the namespace on MSDN, most classes specify the assembly where it resides.
In reference to your previous question:
MSDN Reference
Near the top you get your answer:
Namespace: System.Runtime.Serialization.Json
Assembly: System.ServiceModel.Web (in System.ServiceModel.Web.dll)

Finding references in Visual Studio

I'm using Visual Studio and COM with C# for the first time and there's something I don't quite understand about the 'references'. How do you know what to reference with a given 'using something.something'? The .Net references seem fairly simple, but COM is less obvious. I'm running Visual Studio 2005 and have the latest VSTO installed, but for the life of me, I can't figure out what I need to reference to satisfy:
using Microsoft.VisualStudio.Tools.Applications.Runtime;
There are a few Microsoft.VisualStudio things in the .Net tab, but none that continue with .Tools or anything like it. I feel like I'm missing a key concept here.
There are two issues here -
First, a reference doesn't necessarily correspond to a namespace. A single reference can contain multiple namespaces, and a single namespace can be shared by multiple assemblies which would need to be referenced. Needing to include a reference allows you to use specific types, not entire namespaces.
Second, this is something you'll need to know in advance. If you're using Microsoft's classes, such as the ones in the namespace you listed, MSDN can be a great help.
For example, take Microsoft.VisualStudio.Tools.Applications.Runtime.ServerDocument
If you look at the MSDN page for this class (in that namespace), near the top it includes:
Namespace: Microsoft.VisualStudio.Tools.Applications.Runtime
Assembly: Microsoft.VisualStudio.Tools.Applications.Runtime (in microsoft.visualstudio.tools.applications.runtime.dll)
This specifically tells you which assembly is required.
That reference is part of the Visual Studio SDK. I am currently using VS 2008, but the assemblies should be the same for VS 2005. The link for the SDK is here.
Going the other way is pretty easy. If you're given a reference, you can open that reference in the object browser to see what namespaces it contains, and from that, determine what usings to add. Its not trivial to determine what to reference for a given using, as there's no guarantee that there exists exactly one DLL for each namespace. How is it that you arrived at a
using Microsoft.VisualStudio.Tools.Applications.Runtime;
Without knowing what to reference? If its a code sample somewhere, they ought to mention what the external references and dependencies of the project are.

Categories

Resources