views:

109

answers:

6

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.

A: 

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

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

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.

JaredPar
Ouch, you were two seconds after me and netted a +10. There goes my FGITW theory. :-D
George Stocker
@George, FGITW?
JaredPar
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.
gnomixa
Fastest Gun in the West.
George Stocker
@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?
gnomixa
@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
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).
gnomixa
A: 

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

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

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
it's all included in there, i just used this in my example for simplicity:)
gnomixa
+2  A: 

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%28VS.80%29.aspx

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

Vaibhav