none
how to use dll in C#????

    Question

  • actually, i am new in C# and i am using VS2008 to build a window application. at this stage, i have to build a window form library to import to the main program , the class is built in the dll and the object is built in the main program. is it possible to implement that?how? please state with full example.
    Wednesday, April 15, 2009 2:11 AM

Answers

  • Hi Henry,

    After clicking "Add Reference" in the "References" folder, select the "Browse" tab, then browse for your dll file.

    For the namespace, it really depends what's the namespace in your dll file. But you can get the namespace by this method:

    1. Expand the "References" folder
    2. Double click the dll file your added, a object browser will be shown in the middle of the screen. Your dll related object will be automatically selected.
    3. Expand the object and you will find an object with icon "{}", the text behind is your namespace.

    say if the name space called "Demo.Test1", then what you need to do it add

    using Demo.Test1;

    in your code.

    Regards,
    Steven
    • Marked as answer by Harry Zhu Tuesday, April 21, 2009 7:52 AM
    Wednesday, April 15, 2009 3:25 AM
  • Hi Henry,

    It seems you are trying to create a wrapper API like what I am working on now.

    What you need to do is to create a new project and select "Class" instead of windows form application. Inside the class file, create your own class which contains all the functions you need to create. After building the project, it will be the dll you want.

    To call your class, import your dll and add the using statement. Then you can call your class like:

    YourClass tempClass = new YourClass();
    tempClass.Function1();

    Regards,
    Steven
    • Marked as answer by Harry Zhu Tuesday, April 21, 2009 7:52 AM
    Wednesday, April 15, 2009 4:02 AM
  • Hi Henry,

    You may just think yourself as a API provider from another company. What you do is to sell the API without form design to others.

    So when a company buy your API, it will create a new project and add your dll as reference.

    In my own practice, I will create another project called "Test App" in the same solution with the same Solution with the API project. The two projects are not related to each other. What I do in the test app is to create a user interface to demostrate my API to other programmers.

    Regards,
    Steven
    • Marked as answer by Harry Zhu Tuesday, April 21, 2009 7:52 AM
    Wednesday, April 15, 2009 4:34 AM

All replies

  • Hi Henry,

    What you need is:

    1. Enter the main program
    2. In the view "Solution Explorer" (the one on the right hand side by default), right click the folder "References" and click "Add Reference", then you can browse and insert the dll.
    3. After adding the dll file, remember to add the namespace in the code by "using ..."

    Regards,
    Steven
    Wednesday, April 15, 2009 2:21 AM
  • thank you Steven
    i still have following question
    1. i have already insert the dll which is a windows form control library, am i doing right?
    2. the code"using...."means "using login_v1.dll"(my library is login_v1.dll)?
        i have tried but the error occur,error CS0246: The type or namespace name 'login_v1' could not be found (are you missing a using directive or an assembly reference?)

    3. then just create the object in main, say "UserControl1 object1 = new UserControl1()"(the class in the dll is UserControl1)?
    4. the form design in the dll can be used in the main?

    do you have a simple example?


    thank you for your reply
    Wednesday, April 15, 2009 2:42 AM
  • using namespace;
    using library;

    for instance:

    using System; which is in system.dll
    using System.Windows.forms; which is in System.Windows.Forms.dll

    You reference your dll only in Solution Explorer under References Tab (TreeNode).


    AlexB
    Wednesday, April 15, 2009 3:03 AM
  • What you are doing is different from what steven refer to, you are using UserControl library which is different from Class library. Usercontrol is like Textbox, labels and others control in toolbox that u created.if you said that you create Usercontrol,what you need to do is drag your control on the form and use it. You dont add user control as reference.
    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Wednesday, April 15, 2009 3:06 AM
  • actually, i want to have a caller which doesn't  contain any form design but call the dll which contain form design and program.
    the caller is universal to all user. what i have to do to upgrade the system, is upgrading the dll but not the caller program.
    for this situation, am i doing right to build windows form application, caller and the windows forms control library, login_v1 ?


    to kaymaf
    i am unable to drag the dll on the form in my main program, caller
    Wednesday, April 15, 2009 3:20 AM
  • Hi Henry,

    After clicking "Add Reference" in the "References" folder, select the "Browse" tab, then browse for your dll file.

    For the namespace, it really depends what's the namespace in your dll file. But you can get the namespace by this method:

    1. Expand the "References" folder
    2. Double click the dll file your added, a object browser will be shown in the middle of the screen. Your dll related object will be automatically selected.
    3. Expand the object and you will find an object with icon "{}", the text behind is your namespace.

    say if the name space called "Demo.Test1", then what you need to do it add

    using Demo.Test1;

    in your code.

    Regards,
    Steven
    • Marked as answer by Harry Zhu Tuesday, April 21, 2009 7:52 AM
    Wednesday, April 15, 2009 3:25 AM
  • Hi Henry,

    It seems you are trying to create a wrapper API like what I am working on now.

    What you need to do is to create a new project and select "Class" instead of windows form application. Inside the class file, create your own class which contains all the functions you need to create. After building the project, it will be the dll you want.

    To call your class, import your dll and add the using statement. Then you can call your class like:

    YourClass tempClass = new YourClass();
    tempClass.Function1();

    Regards,
    Steven
    • Marked as answer by Harry Zhu Tuesday, April 21, 2009 7:52 AM
    Wednesday, April 15, 2009 4:02 AM
  • hi steven
    but if i use class library where is my form design?
    Wednesday, April 15, 2009 4:09 AM
  • Hi Henry,

    You may just think yourself as a API provider from another company. What you do is to sell the API without form design to others.

    So when a company buy your API, it will create a new project and add your dll as reference.

    In my own practice, I will create another project called "Test App" in the same solution with the same Solution with the API project. The two projects are not related to each other. What I do in the test app is to create a user interface to demostrate my API to other programmers.

    Regards,
    Steven
    • Marked as answer by Harry Zhu Tuesday, April 21, 2009 7:52 AM
    Wednesday, April 15, 2009 4:34 AM
  • What you are doing is different from what steven refer to, you are using UserControl library which is different from Class library. Usercontrol is like Textbox, labels and others control in toolbox that u created.if you said that you create Usercontrol,what you need to do is drag your control on the form and use it. You dont add user control as reference.
    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    He can also invoke a user control in a standard way. Any draggable control can be invoked at runtime.

    AlexB
    Wednesday, April 15, 2009 3:03 PM