tags:

views:

45

answers:

4

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?

A: 

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

You just include the file when compiling the assembly.

You may have to add a using statement too.

leppie
A: 

Your question is somehow unclear.

When you define a new class, in another dll, it is enough to reference that dll. However you might be unable to access that class because of its accessors. So define you your class with a public keyword.

Sepidar
+1  A: 

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
Do you have to build the assembly file? cant you just reference the myclass.cs file? no? :(
K001
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
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?
K001
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