Click here to Skip to main content
15,884,298 members
Articles / .NET
Tip/Trick

Creating your own library in Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
26 Jan 2012CPOL2 min read 56.4K   7   8
A tip to fellow programmers developing in the .NET environment
It is common when you are new to Visual Studio that you will at some point be writing some good algorithms you want to keep, so you can use them in other projects. Therefore you should always have a library / .dll file you can import to your projects so you don't have to rewrite them over and over for each project you will use them in.

So my hint is: go open up Visual Studio and create a new project. Select class library and name it something catchy like "MyFreakingAwesomeLibrary".

You will now see an auto generated class called "Class1". Go delete that one.

When you have a library, it is good practice to have some namespaces (folders and subfolders) which will hold classes according to their category. Say for instance, you have written some good math algorithms about pythagoras. Then you should add those classes holding those classes to a namespace called trigonometry under another namespace called math.

Well let's say you want to write some pythagoras algorithms like above. Go to your solution explorer and right click your project and select new -> folder. Name that folder "math". Then right click your new folder and select new -> folder. Name that folder "trigonometry". Now right click that folder and select new -> class and name that "pythagoras". You will now see a generated class called pyhagoras. Now go make that public.

You should now have a class looking like this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyFreakingAwesomeLibrary.math.trigonometry
{
    public class Pythagoras
    {
    }
}


If you do, you have done a great job. Look again at this line:
C#
namespace MyFreakingAwesomeLibrary.math.trigonometry

As you can see, you have made your very own library with namespaces now.

When you are coding your library, it is also a good idea to create a test project.
So go to your Solution Explorer and right click your solution and add -> new -> project. Select Windows Forms application and name it "testApp".

Now go back to your Pythagoras class and add this method:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1.math.trigonometry
{
    public class Pythagoras
    {
        public static double GetLengthOfHypotenuse(double a, double b)
        {
            return Math.Sqrt(Math.Pow(a, 2) + Math.Pow(b, 2));
        }
    }
}



Now go back to your solution explorer. Right click your test app and click "set as startup project. Under your test app, right click "references" and click add reference. Under the tab "Projects", select your library and click ok.

Now double click "Form1.cs" and add a button. Double click a random spot within the form and scroll to the top where we have our "using" statements.
Now type in "using <your class="" library="">.math.trigonometry;"

Go back to you form1 design and double click the button.

Insert this line:
C#
MessageBox.Show(Pythagoras.GetLengthOfHypotenuse(2, 3).ToString());

Your code for form1 should look like this:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyFreakingAwesomeLibrary.math.trigonometry;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Pythagoras.GetLengthOfHypotenuse(2, 3).ToString());
        }
    }
}


Now go run your test app and voila. You have now created your first lib.

If you want to use it later on, just add it as a reference to the project and do as we did in this little tip :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError in code Pin
nickcr2-Aug-14 23:39
nickcr2-Aug-14 23:39 
QuestionThanks Pin
shaima'30-Sep-13 20:52
shaima'30-Sep-13 20:52 
QuestionDefining your own class Pin
Member 1001859416-Jul-13 11:54
Member 1001859416-Jul-13 11:54 
AnswerRe: Defining your own class Pin
Chrene9117-Jul-13 2:35
Chrene9117-Jul-13 2:35 
GeneralRe: Defining your own class Pin
Member 1001859417-Jul-13 9:13
Member 1001859417-Jul-13 9:13 
QuestionSource Control and Multiple Project Usage Pin
Michael Haines3-Feb-12 1:12
Michael Haines3-Feb-12 1:12 
AnswerRe: Source Control and Multiple Project Usage Pin
Chrene9117-Jul-13 2:37
Chrene9117-Jul-13 2:37 
GeneralExcellent Advice Pin
yannduran26-Jan-12 17:46
yannduran26-Jan-12 17:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.