In this article we will learn that how to make as assembly in C#.net or how to make class library in C# using visual studio 2008.
Follow these steps to create an assembly:
- From File menu, click ‘New project’ (ctrl + N).
- From Templates window, select Class Library.
- Enter valid name of your Class Library (in this example we enter ClassLibrary1).
- Click Ok.
- Rename Class1 to Rectangle.
- Write a new function ‘GetRectangleArea’ in ‘Rectangle’ class.
- Save project, click File menu and then Save All.
- Build the project using Build menu, click on Build Solution.
Your class library is created successfully with class Class1.
using System; using System.Collections.Generic; using System.Text; namespace ClassLibrary1 { public class Class1 { } }
using System; using System.Collections.Generic; using System.Text; namespace ClassLibrary1 { public class Rectangle { public int GetRectangleArea(int Width, int hieght) { int Area = Width * hieght; return Area; } } }
GetRectangleArea function takes two parameters (both are integer, it makes a calculation and then returns the result as area of rectangle.
And now, next is to use this assembly or class library in windows application. Follow these steps:
- Add a new project (File–>Add–>New Project)
- From Templates window, select Windows Application.
- Enter valid name of your Windows Application (in this example we enter WindowsApplication1).
- Click Ok.
- Add reference of ClassLibrary1 in windowsApplication1 (see below)
- Generate click event of button (you can generate click event through double click on button control)
Form1 will be appears. Add controls on this form like this:
Write below code in the ButtonCalculate_Click
private void ButtonCalculate_Click(object sender, EventArgs e) { ClassLibrary1.Rectangle classRectangle = new ClassLibrary1.Rectangle(); textBoxArea.Text = (int.Parse(textBoxHeight.Text) * int.Parse(textBoxWidth.Text)).ToString(); }
you can run the WindowsApplication1 (make startup project as WindowsApplication1)