This article shows how to create a Class Library project and include it in a web application project. ASP.NET is part of the Microsoft .NET Framework. To build ASP.NET pages, you need to take advantage of the features of the .NET Framework. A Class Library is one of them. A Class Library contains one or more classes that can be called on to perform specific actions. Creating a Class Library makes a DLL that can be used for reusability.

Creating Class Library

Now open the Visual Studio and select "File" -> "New" -> "Project..." -> "Class Library". The following code will be generated:

Class Library1.jpg

Now click on the "Ok" button. The Solution Explorer contains C# classes (class.cs) in your project. Now replace the default code (shown above) with the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ClassliberaryDemo
  6. {
  7. public class PersonalDetail
  8. {
  9. private string _firstname;
  10. private string _lastname;
  11. private string _Email;
  12. private long _PhoneNumber;
  13. public string FirstName
  14. {
  15. get { return _firstname; }
  16. set { _firstname = value; }
  17. }
  18. public string LastName
  19. {
  20. get { return _lastname; }
  21. set { _lastname = value; }
  22. }
  23. public string Email
  24. {
  25. get { return _Email; }
  26. set { _Email = value; }
  27. }
  28. public long PhoneNumber
  29. {
  30. get { return _PhoneNumber; }
  31. set { _PhoneNumber = value; }
  32. }
  33. }
  34. }

Now build this project. After building this project, you will see ClassLibrary1.dll in your project's bin/debug directory.

Class-Library2.jpg

Now open the Visual Studio again and select "File" -> "New" -> "Project..." -> "ASP.NET web application"

Class-Library3.jpg

Now add the reference of the preceding created DLL. To add a reference right-click on the project then seelct "Add Reference".

Class-Library4.jpg

Now browse to the ClassLibrary1.dll file and add it to the application.

Class-Library5.jpg

Use the following procedure to call the properties of your component.

Using namespace

Add a using statement (as in the following) for ClassliberaryDemo to the beginning of your project.

  1. using ClassliberaryDemo;

Create an Object of the "PersonalDetail" class, as in the following:

  1. PersonalDetail PD = new PersonalDetail();

Now Calling Properties

Class-Library6.jpg