Results 1 to 4 of 4

Thread: Very Rough Tutorial on how to make a simple DLL file and use it

  1. #1
    Administrator
    Join Date
    Sep 2012
    Posts
    213
    Bots Posted
    14
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)

    Very Rough Tutorial on how to make a simple DLL file and use it

    To create a DLL with functions that can be used in multiple projects. Follow these simple steps.

    1. Open Visual Studio and create a new Class Library Project.
    a. The Name/Solution Name you give this Project will be the namespace you must import after the dll is added to any project you use it in. Keep this in mind. We’ll call this class BotTools
    b. 01-NewProject.jpg

    2. Once the project is open, you will be presented with a Class1.cs file. I always delete/rename this as you’ll want to group your functions into easy to recognize names. For this tutorial I’ll make a class that will provide you with a random city name. This should be pretty easy to follow.
    a. Since we are providing a method to return a random city, the first class file will be name Cities this class needs to be declared as public since we will want to be able to access some of the functions externally (IE from the project we will add the DLL to later). This class will have 2 functions. getCityName and getCityList
    getCityName will be a public function that will be able to be accessed externally
    getCityList which will be private since we only need to access it from inside this class
    b. 02-CitiesClassFile.jpg

    3. Now that we have all the code we need for the DLL, we need to set the assembly and file version number of the project (ALWAYS REMEMBER TO SET THIS) as it will ensure that people are using the most up to date version of your class. Since this is the first build I am leaving it at default 1.0.0.0
    03-AssemblyInfo.jpg

    4. Once you’ve set the proper version number it’s time to build the project. Click Build then Build Solution. If the build was successful and there were no errors, you should be able to navigate to the folder where the project resides and in the Debug folder you will find the DLL file.
    04-Dll BotTools.jpg
    CONGRATS! You’ve made your first DLL… But how do we use it?



    5. To use the DLL file I’ll make a simple program that when you click a button will display a random city name. Open a new copy of visual studio and this time create a new Windows Forms Application or Console Application (choice is yours) For this demo I’ll make a windows forms application.
    a. We will name it UsageOfBotTools
    b. First thing we will do is add a Button and a textbox to the form. For this demo I’ll keep the default names of the controls (textBox1 and button1)
    05-WindowsForm.jpg

    6. Next we need to add a reference to the BotTools.dll found in the debug folder of the Class Library project.
    06-AddDLL.jpg

    7. After we have the dll added as a reference, open the code view of the Form1.cs file and at the top add a reference to the dll
    07-AddUsingStatement.jpg

    8. Now add the OnClickEvent for the button on the form. (you can easily do this by switching back to the form view and double clicking on the button.

    9. Once in the code of the click event you need to declare a new variable to hold the Cities class file like so BotTools.Cities cities = new BotTools.Cities();

    10. Next set the textbox text to equal the returned value of the getCityName function.. Notice how as you type cities. You do not see getCityList as function available to use (this is because we made it private.
    08-getCityName.jpg


    11. YOUR FINISHED… RUN THE APPLICATION AND CLICK THE BUTTON….
    Make sure to include the DLL file with the exe or it will not work for other people

  2. #2
    Administrator
    Join Date
    Sep 2012
    Posts
    213
    Bots Posted
    14
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Please see attachment for both projects
    Attached Files

  3. Than/ks »Scapegoat« thanked for this post
    Likes »Scapegoat« liked this post
  4. #3
    Senior Member trianlockhart's Avatar
    Join Date
    Jun 2011
    Posts
    232
    Bots Posted
    0
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pretty awesome of you to share this Kardsen. Thanks man. =)

  5. #4
    Member
    Join Date
    May 2012
    Posts
    87
    Bots Posted
    6
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very nice little TUT to expand my C# Knowledge. Appreciate it alot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •