Welcome To My Blog

This blog contains posts that may benefit ordinary visitors or programmers (particularly .NET programmer).
To view posts related to particular subjects, click the link under Labels.

Tuesday, February 17, 2009

Using Private Assembly in C# VS2005

This article is intended to beginner/medium level programmer.

Before we continue, let me explain first about the term "assembly" in C#. An assembly is simply a project that you have compiled either into a portable executable or DLL file. If you are using Visual Studio 2005 IDE to create your project, you will notice there are so many project templates that you can choose from. The templates range from Console applications, Windows application, Mobile applications and Web applications.

A project can only be compiled into an EXE assembly if it has the Main method, or else it will be a DLL assembly only.

The assemblies are further categorized into two types - private or shared assemblies. In this article, I will focus on the Private Assembly.

A private assembly may have the extension EXE or DLL. It is termed "private" because the assembly is not shared. Meaning that if you make a reference to a private assembly in your new project, you have to copy the private assembly to the bin folder of your new project. If you are using the VS2005 IDE, copying is done automatically when you Add Reference to the private assembly in the new project. Hence, every time we make a reference to a private assembly, we make a copy of that assembly in the new project.

To understand how to use a private assembly in C#, let we do the following steps to create a class library:
  1. Open VS2005 and create a new project. Make sure you select Visual C# --> Windows --> Class Library.

  2. Named the project as Converter. After a while, the code window appears.

  3. Rename the Class1.cs to Convert.cs in the Solution Explorer window.

  4. Add two methods to the class. One to convert temperature from Celcius to Fahrenheit and another to convert from Fahrenheit to Celcius.
  5. Click the Build menu and choose Build Converter to compile the project.

Assuming that you have created your project in the Visual Studio 2005 default folder, the assembly "Converter.dll" can be found in the folder My Documents --> Visual Studio 2005 --> Projects --> Converter --> bin --> debug/release.

Now let's try to use the created assembly in our new project. Follow the steps below to start a Windows application:

  1. In Visual Studio 2005 IDE, start a new project. From the "New Project" dialog box, select Visual C# --> Windows --> Windows Application template.

  2. Add controls to the form as shown.

  3. Named the control respectively as follows:
    radioButton1 --> Name: rbnCelciusToFahrenheit, Text: Celcius To Fahrenheit
  4. radioButton2 --> Name: rbnFahrenheitToCelcius, Text: Fahrenheit To Celcius
    label1 --> Name: lblInstruction, Text: (blank)
    label2 --> Name: lblResult, Text: (blank)
    textbox1 --> Name: txtTemperature, Text: (blank)
    button1 --> Name: btnCalculate, Text: Calculate

  5. Right click "References" in the Solution Explorer window and select "Add Reference...". Click the Browse tab in the Add Reference dialog box. Navigate to the folder containing "Converter.dll" private assembly that you have created earlier. After clicking OK, you should see the name Converter appears under the References list. If you click the Converter, you notice Copy Local = true in the Properties window.

  6. Double click the btnCalculate. VS2005 IDE takes you to the code window and gives you the event handler named btnCalculate_Click. Add the code in the event handler as follows:

  7. Get back to the Form1 (design). Then, double click the rbnCelciusToFahrenheit and rbnFahrenheitToCelcius radio buttons. You will get the "
    rbnCelciusToFahrenheit_CheckedChanged" and "
    rbnFahrenheitToCelcius_CheckedChanged" respectively. Add codes to the both event handlers as shown:
  8. Now, run your program. Notice that even though the method for converting the temperature is not in the new project, we still be able to execute the code because we have added reference to the Converter assembly.

From the above practice, you see how easy it is to reuse codes that we have done earlier in other project.