Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import Other Namespaces into Visual C#?

Sorry if my question seems a little noobish but I can't find an answer. I would like to use DirectInput for my XNA game. I've read around and it seems this is the best way to get input from a gamepad other than a XBox 360 controller. The first step is to include the Microsoft.DirectX.DirectInput namespace however I don't quite know how. Simply putting it in at the beginning of my class doesn't work, visual C# doesn't recognize it.

This got me thinking should I download the DirectX SDK? There is alot of stuff in there and the file is big how do I just download the single namespace? Is this even possible? If I can download the single namespace where do I put the file?

Now you can see all the questions racking around in my brain. So how do I make visual C# recognize the Microsoft.DirectX.DirectInput namespace so I can use the methods, properties and all that good stuff?

Thanks!

like image 352
MrSplosion Avatar asked Feb 17 '12 23:02

MrSplosion


People also ask

How do I change a namespace in Visual Studio?

Place your cursor in the namespace name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Change namespace to <folder name>.

Can we use two namespaces in C#?

A class in C# is fully known by its respective namespace. Note: Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name.

Can we call namespace from different project in C#?

Yes, add a reference to Project1 from Project2. Right-click the project, choose "Add References" then from the "Projects" tab, choose Project1.


2 Answers

You need to set a reference to the DLL containing the code library you'd like to use. Presumably that's part of the SDK, so yes, you should download it. Then, in Visual Studio

  • open the solution explorer
  • open the project where you want to use the library
  • right-click the references node for that project
  • choose "add reference"
  • navigate to and select the dll

Once you've added the reference successfully, VS should recognize the using directive (and any types you've used from the assembly).

Note that references are made to assemblies; assemblies are not the same as namespaces. Beginners often confuse the two. Types that are defined in different assemblies can be in the same namespace; the BCL has dozens of examples of this.

Furthermore, the "using" directive doesn't cause anything to be loaded or anything like that; it just allows you to specify types without fully qualifying the names. In fact, it's possible to code in C# without ever using a using directive. For example, the following code files are equivalent:

using System.Collections.Generic;
public static class Collector
{
    public static ICollection<T> Collect(IEnumerable<T> enumerable)
    {
        return new List<T>(enumerable);
    }
}

AND

public static class Collector
{
    public static System.Collections.Generic.ICollection<T> Collect(System.Collections.Generic.IEnumerable<T> enumerable)
    {
        return new System.Collections.Generic.List<T>(enumerable);
    }
}
like image 200
phoog Avatar answered Oct 07 '22 17:10

phoog


Download the SDK. It will not be redistributed with your app.. but everything you need to dev DirectX apps will be included in there. When installed there will be new entries in the "Add Reference" dialog.

like image 41
Sam Axe Avatar answered Oct 07 '22 17:10

Sam Axe