0

I created a C# file and wish to compile it into a DLL for future use. However, this .cs file depends on another DLL. In my code inside my .cs file I am doing something like:

using anotherlib.dll;

When I try to compile it into a DLL, the compiler tells me that it can't find that anotherlib.dll (missing directive or assembly reference).

What is the right way to go about it?

I am using .NET 2.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sarsnake
  • 26,667
  • 58
  • 180
  • 286

7 Answers7

4

You need to add a reference to that particular DLL.

If you are using Visual Studio try the following

  1. Right click on your project in solution explorer
  2. Select Add Reference
  3. Go to the Browse tab
  4. Navigate to the DLL location on disk and select OK
  5. You might need to add a using statement to the desired namespace

If you have the source for the DLL, it's much better to use a project reference than a file reference. Simply add the project to the same solution, repeat steps 1-2 above and select Projects instead of Browse.

If you are not using Visual Studio, then you need to pass the fully qualified path of the DLL to the compiler with the /r: flag.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Ouch, you were two seconds after me and netted a +10. There goes my FGITW theory. :-D – George Stocker Oct 05 '09 at 17:01
  • aha, that's probably it. The project in VS compiles and runs no problem. But when I compile into dll through the system prompt, that's when it doesn't see the reference. – sarsnake Oct 05 '09 at 17:02
  • @JaredPar: ok, so if I compile it like you are saying passing the full path to it, does this mean that if this dll is used in production apps, this same path is going to be looked for? – sarsnake Oct 05 '09 at 17:05
  • @gnomixa, there are 2 issues here. 1) compilation of the DLL which needs the full path as it exists on your development machine. 2) loading of the DLL in production applications. #2 uses a mix of operating system and CLR rules to find the DLL but both are independent of the path used in #1. The simplest solution is to deploy the DLL in the same directory. – JaredPar Oct 05 '09 at 17:13
  • ok. that answers my question. I just wanted to make sure that if I add a reference to my new dll that's located on my machine, there won't be production issues if I add the referenced dll in the bin folder in production(ie same directory as the newly baked dll). – sarsnake Oct 05 '09 at 18:11
2

You need to reference it using /r. If you are using the command line compiler. Here's a link: http://msdn.microsoft.com/en-us/library/ms379563(VS.80).aspx

If you are using Visual Studio, you simple add it as a reference in your project.

Vaibhav
  • 11,310
  • 11
  • 51
  • 70
1

A using statement is for importing a namespace. You must also add an assembly reference to actually use the namespace. If you are using csc.exe from the command line, you can specify an assembly reference with the command line argument /reference:filename.dll. If you are using Visual Studio, you can right click on your project and select "Add Reference...".

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
1

You don't use the using statement in C# that way.

Using, in C#, refers to the namespace. You "include" the other DLL by "referencing" it in your project or compiler. If you're using Visual Studio, add "anotherlib.dll" as a project reference, and then do:

using TheNamespaceFromAnotherLibDLL;
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

You need to right click on the project in "Solution Explorer" and click on "Add Reference".

Browse for its location and add it as a reference.

This MSDN reference should provide more information.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
0

Instead of saying "using" in your code, add it as an assembly reference. In Visual Studio right-click on "References" and add the DLL. Then in your code have "using" for the namespaces of the stuff in the DLL.

Robert Fraser
  • 10,649
  • 8
  • 69
  • 93
0
  1. Firstly, you need to add assembly reference in your project.
  2. In Visual Studio right-click on "References" and add the DLL.
  3. After that you may access that DLL in your code by using keyword

And one more thing is that, You may put that DLL( You are accessing in your code ) in the bin folder of your project where your project DLL is generating. Because suppose you are providing your DLL to others so you can easily give the bin folder. So He/She friendly use your DLL. And Never get error due to dependent DLL.

Sonu Rajpoot
  • 485
  • 4
  • 6