Page 1 of 1

Creating a DLL and using it in your Application The following is an easy example of making a DLL, and then using it in Rate Topic: ***** 1 Votes

#1 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 375
  • Joined: 17-July 09

Post icon  Posted 20 August 2009 - 06:34 AM

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 :D. Click OK.


Step 2
You will be presented with
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.
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.
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.

Is This A Good Question/Topic? 2
  • +

Replies To: Creating a DLL and using it in your Application

#2 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 375
  • Joined: 17-July 09

Posted 20 August 2009 - 05:54 PM

Screenshots (Forgot to add them when I transfered sorry).
Attached Image

Attached Image

Attached Image

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Aukatrau*


Reputation:

Posted 06 March 2010 - 09:26 AM

Thanks for the tutorial. I never knew how to do this before.
Was This Post Helpful? 0

#4 T.ALAKHTIRI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-March 09

Posted 31 March 2010 - 11:51 AM

Thanks a lot for this great tutorial.
Was This Post Helpful? 0
  • +
  • -

#5 philip_john  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 04-February 11

Posted 07 February 2011 - 04:25 AM

I want to ask question because I have no idea what DLL is and what is the purpose of DLL. I heard about DLL but I have no idea on it. Hope to answer my question.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Posted 07 February 2011 - 04:40 AM

Research is skill philip_john, practise it some more.
Wikipedia Entry

This post has been edited by AdamSpeight2008: 07 February 2011 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1