Monday, June 18, 2012


Source Code References Project

References are used for using code that are outside the project. The most known example is a .dll file. In Visual Studio you can create a .dll file by creating a Class Library project. It's basically an application without a user interface, so users can't interact with it. I personally use .dll files for code I use a lot for other projects. It is also used for logically separating code.

I will use the words project and library intertwined, because when you develop a library it is a project within Visual Studio. This does not mean that every project is a library. I am implying that every library is a project when developing one in Visual Studio. By calling it a project, I hope you will understand that a library is nothing more than a collection of classes, just like a console application. The only difference is that a console application has a user interface and a library does not.

Create a new console application called Reference Tutorial.

To start off with the tutorial, I would like to point out that you already used references in the "Hello World!" tutorial. In the Solution Explorer (1) you see all the references that are currently made in your current project. For example there is a project (library) called System which has been made a reference to and as you can see many more: System.Core, System.Data, etc... You can compare the project System with your current project. Your current project is called Reference Tutorial and the project that you are using outside your project is called System. The only difference is that you can't change the code that the project System contains, because Microsoft  does not like it when other people can see/change their code.

Just referencing it to your project is not enough in order to use the methods System can provide. You also have to point it out in the class its file (2) if you want to use it in that specific class file. Yes, this means that you have to type "using <project name>;" in every class file you plan on using your outside project.

As a summary and the only things you have to do:
  • You have to create a reference inside your project (1)
  • You also have to tell the compiler you are using that reference in every class file you are going to use the reference. (2)

References are basically a one sided relation between two projects. With a reference you use an outside project without the outside project knowing that you are using it. It's like me controlling the hand of a paralyzed person. I am the current project and the paralyzed person is the outside project and I'm using that person's hand. The paralyzed person also does not notice me using his hand. 

Let's start by creating your own reference. First you will need to add a new project. You can do this by right clicking on your solution in the Solution Explorer (1) -> Add (2) -> New Project (3).

Once you've done that; add a Class Library project by clicking on it (1). Then fill in a name for the project: Reference Library (2). Click OK when you're done (3). It's basically the same when you create a new Console Application. As you see you can also add a new console application to the solution.

A Class Library project will be added to the solution. By default a class will also be added to the library, called Class1.

Start by renaming the Class1 file to Calculator by right clicking Class1 and selecting Rename. Select Yes if it asks if you also want to rename the class itself (1). Once you have done that, add the following code (2):
public int Sum(int a, int b)
     return a + b;
In order to use the Sum method in your Program class, in the Reference Tutorial project, a reference has to be made.

Add a reference by right clicking your Reference Tutorial project in the Solution Explorer (1) and then clicking Add Reference (2).

You can add various kinds of references (1):
  • .NET
    • The .NET Framework has a lot of libraries that you can use. System is one of them.
  • COM
    • Here you can find the libraries that applications you have installed provide. For example I have VLC media player installed and I can use their libraries to create my own extended VLC media player.
  • Browse
    • You can browse for files on your computer and select it if it is a valid library. This does not mean you can select and use a .dll file in the Windows folder. That is because some companies do not want other people to use their code. If you could, they wouldn't be able to make money! If you build the Reference Library project, it creates a .dll file and you can select that file, but then you wouldn't be able to change the code that easily in the Reference Library.
  • Projects
    • Here you can select a project in your solution that you wish to create a reference to. You can use almost ANY project type as a reference. This also means console applications.
Click on the tab Projects and select the Reference Library if it is not selected yet (2) and then click OK (3).

You will now see that the reference has been added to your project in the Solution Explorer (1). Go to your Program class (Program.cs) and add the following under "using System.Text;":
using Reference_Library;
Now a reference has been made in your project and in the Program class file.

Now add the following code:
Calculator calculator = new Calculator();
int sumOutcome = calculator.Sum(5, 4);
As you can see, initializing an object and calling its method is the same with a reference.

The problem of using an outside project is that you don't know the classes and methods it contains. Now you do, because you made it yourself. The Object Browser will show the classes and methods the outside projects contain. You can open the Object Browser by going to View -> Object Browser.

As you can see and already know, your outside project Reference Library has a class called Calculator with a method called Sum. This way you can also explore System and of course all the other references you made. Maybe you can find a reference in the COM tab of an application you like and use their library, which you can fiddle around with it. Just remember:
  • You have to create a reference inside your project
  • You also have to tell the compiler you are using that reference in every class file you are going to use the reference (using <project name/library name>;).
Source Code References Project

>> Polymorphism Interfaces


  1. So this is also how engines for programs work that you have to install beforehand, so that the program has access to all the functionality it needs?

  2. Exactly! I personally compare it with DirectX. A lot of games have a reference to DirectX its libraries.