p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual C# 2005 (http://p2p.wrox.com/forumdisplay.php?f=222)
-   -   C# DLL (http://p2p.wrox.com/showthread.php?t=71665)

Bill_Thompson November 30th, 2008 04:31 PM

C# DLL
 
After someone creates a DLL in C# using the Microsoft Visual development environment, how would another programmer take that code, make a new project that includes the DLL's source and make a GUI that uses the DLL'S API?


dparsons November 30th, 2008 10:26 PM

To be clear the second developers project would not contain the source of the DLL, only the DLL itself. To utilize this DLL in the project the developer would right click on the project and select Add Reference and then navigate to the location to the DLL.

Once the reference is added they could do something like
Code:

using SuperCoolDLL;
namespace Foo
{
public class Boo
{
     private void SomeMethod()
     {
     SuperCoolDLLNamespace.SuperCoolDLLClass b = new SuperCoolDLLNamespace.SuperCoolDLLClass();
     b.DoSomething();
     }
}
}

I should point out, unless there is a naming conflict between this third party assembly and the .NET Framework, you will not need to reference the class by Namespace.Class as I did above, you can simply reference the class. (If you do have a naming conflict you will need to fully qualify the class otherwise you will get a '<className> is ambiguous...' error)

Lastly, the using statement should contain the name of the namespace(s) within the DLL that you wish to use and not the name of the physical DLL.


hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

Bill_Thompson December 1st, 2008 07:39 PM

Well, I have access to the source for the DLL code as well as the project file.

I have done a few things. I have created a console app to use as the front-end to the project. Then I have added the DLL as a reference like this:

1) In the Solution Explorer right-click "References" and select "Add Reference ...".

2) Select the "Browse" tab.

3) Navigate to the DLL and select it.

4) Add the appropriate using directive to the code file(s) where you want to use the DLL.


I have also found that I can add the Project that the DLL was made in into my new Solution file.

I have included the namespace of the DLL in the "using" section of my code.

And the intelligent type I am using recognizes this class when I declare it in my code.

But it does not seem to recognize all the methods (api's) of the class.

What am I doing wrong?

http://i67.photobucket.com/albums/h2...st_problem.jpg

Strangely, "Equals", "GetHashCode", "GetType" and "ToString" are not seen in the DLL's source code for available methods for this class.

Also, I know that if you double click on the added reference, an Object Explorer should come up showing the available methods. This is not what happens:

http://i67.photobucket.com/albums/h2...no/objects.jpg


dparsons December 1st, 2008 08:13 PM

How are the methods defined? Like this:

public static void MethodName() {}

or this:

public void MethodName() {}

Only methods declared with the static keyword can be called like this:

Code:

TheClass.SomeMethod();
Otherwise you need to do something like this (these are known as instance methods):

Code:

TheClass class = new TheClass();
class.SomeMethod();

Methods declared with private or intern won't be accessible. In the case of the latter, this will only be true if the class within the DLL is in a different namespace then the calling class.

hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========


All times are GMT -4. The time now is 10:51 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.