2

If you use a you assembly reference (myExample.dll), you add like this to the top

using myExample;

Now if you create a class file, how do you reference it?

marr75
  • 5,666
  • 1
  • 27
  • 41
001
  • 62,807
  • 94
  • 230
  • 350

4 Answers4

10

Well, in your class file you have the following:

namespace myNamespace
{
    public class MyClass
    {
        public void MyMethod() { }
    }
}

Let's assume that you have this in an assembly named MyDll.dll. You'd use it as follows:

  1. You add a reference to MyDll.dll within the solution explorer
  2. You include the namespace with using myNamespace;
  3. Then you can use your class doing MyClass test = new MyClass();

If you don't add the namespace like I said in 2., you'd use your class like:

myNamespace.MyClass test = new myNamespace.MyClass();
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Do you have to build the assembly file? cant you just reference the myclass.cs file? no? :( – 001 Aug 30 '10 at 11:27
  • You don't include `.cs` files. The compiler takes all the files that belong to a project, compiles them, the linker links them into an assembly (roughly). Then you reference the assembly from another project. By `using` a namespace, you have access to any public class just by using its name! Just think about .NET classes - you don't include or reference a certain `.cs` file to use the `Form` class or the `String` class, do you? – Thorsten Dittmar Aug 30 '10 at 14:45
  • What I mean is, I write my code in main.cs but want to reference a class which is a class.cs file. In main.cs how do I use methods from class.cs? – 001 Aug 31 '10 at 14:04
  • 1
    By creating an instance of the class that you defined in `class.cs` and using that instance! Forget about the `.cs` files!! If you add a new `.cs` file to your project, you'll define a class in it as I wrote in my answer above. Then the compiler compiles the file. In your "main.cs" you do not use the `.cs` file, you use the class you defined. I suggest you just *try* it. Create a solution in Visual Studio, add a new class and in your `Program.cs` just create an instance of that class. Same for DLLs, but you have to add a reference to the DLL in your solution. – Thorsten Dittmar Sep 02 '10 at 12:09
0

You want to add a using statement for whatever namespace you want to import. Go to the file and see what namespace it wraps the class you're interested in.

marr75
  • 5,666
  • 1
  • 27
  • 41
0

You just include the file when compiling the assembly.

You may have to add a using statement too.

leppie
  • 115,091
  • 17
  • 196
  • 297
0

Your question is somehow unclear.

When you define a new class, in another dll, it is enough to reference that dll.

Please note that you might be unable to access that class because of its accessors. Define your class with a public keyword.

udidu
  • 8,269
  • 6
  • 48
  • 68
melmi
  • 988
  • 3
  • 9
  • 27