School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,118 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,987 people online right now. Registration is fast and FREE... Join Now!




Creating a DLL and using it in your Application

 
Reply to this topicStart new topic

> Creating a DLL and using it in your Application, The following is an easy example of making a DLL, and then using it in

PDUNZ
Group Icon



post 20 Aug, 2009 - 05:34 AM
Post #1


Step 1
Create a new project but instead of selecting Windows Forms Application, you want to select Class Library as shown below. I have given it a name of PDUNZDLL biggrin.gif. Click OK.


Step 2
You will be presented with
CODE

Public Class Class1

End Class


Im going to change Class1 to another name, lets say MyFunctions. So Public Class Class1 has been changed
to Public Class MyFunctions.

Step 3
Im just going to program a simple Math function, which will use 2 textboxes that I will have in my application, and add the 2 numbers together and then display the result in a label.

So, I created a new function in the class and added in the required code. So, now the code looks like this.
CODE

Public Class MyFunctions
    Public Function AddMyValues(ByVal Value1 As Double, ByVal Value2 As Double)
        Dim Result As Double

        Result = Value1 + Value2

        Return Result

    End Function
End Class


As you can see, I have created a Function called AddMyValues with ByVal Value1 as Double which will be the holder for the value being passed from textbox 1, and the same for Value2 which would hold the value being passed from textbox 2.

Step 4
Ok, thats pretty much it for the DLL so now we can go ahead and save the project, and then build it just like for an application. Now goto the projects Bin/Debug directory and you will find a DLL that you have just created.


Step 5
Now lets create an application to use it. Create a new application like you would normally do and create a form similar to the one shown. I am going to leave the default names for the textboxes, but I have changed the name of the Label to lblResult and the name of the button to btnAdd.


Step 6
Now you will have to add a reference to your newly created DLL, Im sure you know how to do that by now - Project -> Add Reference, and browse to where your DLL is located, select it and click ok.

Step 7
Now lets do some coding. First you need to import it as you would with others. So, above Public Class Form1 type Imports and a list should show up, and select your DLL. Remember I named mine PDUNZDLL - Imports PDUNZDLL.

Step 8
Now in the Button_Click event, I added this line Dim Add As New PDUNZDLL.MyFunctions. As you can see I declared a name Add for MyFunctions in my DLL (look back at the DLL code. This is so I dont have to type PDUNZDLL.MyFunctions.AddMyValues, instead I can just use Add.AddMyValues.

Now we will use that variable to call the required function on the DLL that I want to use. The whole code is shown below. Study it and you will see how Add is used. Also look back at the DLL code at the start to fully understand it.
CODE

Imports PDUNZDLL
Public Class Form1

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim Add As New PDUNZDLL.MyFunctions

        lblResult.Text = Add.AddMyValues(CDbl(TextBox1.Text), CDbl(TextBox2.Text)).ToString

    End Sub
End Class


Step 9
Now the code is done, save the project and then run the application. Enter 2 numbers and then press the button and the result - which was done in the DLL will be displayed in your result label.


Thanks for reading.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

PDUNZ
Group Icon



post 20 Aug, 2009 - 04:54 PM
Post #2
Screenshots (Forgot to add them when I transfered sorry).
Attached Image

Attached Image

Attached Image

Attached Image
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 01:33PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month