Step 1Create 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

. Click OK.
Step 2You 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 3Im 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 4Ok, 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 5Now 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 6Now 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 7Now 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 8Now 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 9Now 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.