แชร์ผ่าน


Walkthrough: Creating a Web Application Project with a Class Library

Classes provide reusable code in the form of objects. A class library contains one or more classes that can be called on to perform specific actions. This walkthrough shows how to create a class library project and incorporate it into a Web application project.

Prerequisites

In order to complete this walkthrough, you will need:

  • The .NET Framework version 3.5.

  • The SP1 release of Visual Web Developer 2010 Express.

The following procedures build upon each other. Therefore, you must follow the order of the procedures to successfully complete this walkthrough.

Creating the Class Library Project

To create the CDemoLib class library and the Customer class file

  1. On the File menu, select New Project to open the New Project dialog box.

  2. In the list of Windows project types, select Class Library, and then type CDemoLib in the Name box.

    Note

    Always specify the name of a new project when you create it. Doing this sets the root namespace, assembly name, and project name, and also makes sure that the default class will be in the correct namespace.

  3. In Solution Explorer, right-click CDemoLib and then click Properties.

    Notice that the Default namespace box contains CDemoLib. The root namespace is used to qualify the names of class in the assembly. For example, if two assemblies provide class named Customer, you can specify the Customer class by using CDemoLib.Customer.

    Close the properties window.

  4. In Solution Explorer, right-click CDemoLib, click Add, and then click Class.

    The Add New Item dialog box is displayed.

  5. Type Customer.cs in the Name box and then click Add to create the class.

    A class named Customer is added to your class library.

  6. In Solution Explorer, right-click Class1.cs and then click Delete.

    This deletes the default class that is provided with the class library, because it will not be used in this walkthrough.

  7. In the File menu, click Save All to save the project.

Creating the Class

Constructors control the way your class is initialized. Properties are values of the class that you can get and set. In Visual C#, all constructors have the same name as the class.

Note

The access level of the constructors determines which clients will be able to create instances of the class.

To add code to define the Customer class

  • In the code editor, replace the existing code with the following code to define the Customer class in the CDemoLib class library.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CDemoLib
    {
        public class Customer
        {
            private int _age;
            private string _name;
    
            public Customer()
            {
                Age = 0;
                Name = "Not available";
            }
    
            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }
    
    Imports System 
    Imports System.Collections.Generic 
    Imports System.Linq 
    Imports System.Text 
    
    Namespace CDemoLib 
        Public Class Customer 
            Private _age As Integer 
            Private _name As String 
    
            Public Sub New() 
                Age = 0 
                Name = "Not available" 
            End Sub 
    
            Public Property Age() As Integer 
                Get 
                    Return _age 
                End Get 
                Set 
                    _age = value 
                End Set 
            End Property 
            Public Property Name() As String 
                Get 
                    Return _name 
                End Get 
                Set 
                    _name = value 
                End Set 
            End Property 
        End Class 
    End Namespace
    

Adding the Class Library to a Web Application Project

To test the class, you must have a project that uses it. This project must be the first project that starts when you run the application.

To add the CDemoTest Web Application project as the startup project for the solution

  1. In the File menu, click Add and then click New Project.

    The Add New Project dialog box is displayed.

  2. Under Project types, expand Visual Basic or Visual C#, and then click Web to display available Web templates.

  3. In the Templates box, select ASP.NET Web Application.

  4. In the Name box, type CDemoTest as the name of the new application.

  5. Click OK.

    creates the Web application project in the existing solution.

In order to use the Customer class, the client test project must have a reference to the class library project. After you add the reference, it is a good idea to add a using statement in C# (an Imports statement in Visual Basic) to the test application to simplify the use of the class.

To add a reference to the class library project

  1. In Solution Explorer, right-click the References node underneath CDemoTest, and then click Add Reference.

  2. In the Add Reference dialog box, select the Projects tab.

  3. Double-click the CDemoLib class library project. CDemoLib will appear under the References node for the CDemoTest project.

  4. In Solution Explorer, right-click Default.aspx and then click View Code.

Adding the reference to CDemoLib lets you use the fully qualified name of the Customer class, which is CDemoLib.Customer.

To add a using or Imports statement

  • Add the following using statement (Imports in Visual Basic) at the top of the code editor window for the Default.aspx page.

    using CDemoLib;
    
    Imports CDemoLib
    

    Adding this statement lets you omit the library name, and refer to the class type as Customer.

Using the Class from the Class Library

The CDemoTest Web application will call the class that is contained in the class library and display the results.

To add code to create and use a Customer object

  1. In Solution Explorer, right-click Default.aspx and select View Designer.

  2. From the Standard tab of the Toolbox, drag a Label control onto the design surface.

  3. Double-click the design surface to display the Page_Load event handler.

  4. In the Page_Load event handler add the following code:

    Customer myCustomer = new Customer();
    myCustomer.Name = "Kevin Browne";
    myCustomer.Age = 42;
    Label1.Text = "Name: " + myCustomer.Name +
        "<br/>Age: " + myCustomer.Age.ToString();
    
    Dim myCustomer As New Customer() 
    myCustomer.Name = "Kevin Browne" 
    myCustomer.Age = 42 
    Label1.Text = "Name: " & myCustomer.Name + "<br/>Age: " & _
         myCustomer.Age.ToString()
    
  5. In the File menu, click Save All to save the solution.

To run and debug the CDemoTest project

  1. Press CTRL+F5 to start the solution.

    Notice that the Customer class properties are automatically displayed in the label control.

  2. Close the browser window to return to the development environment.

Next Steps

This walkthrough illustrates the basic process of adding a class library to a Web application project. You might want to expand this example by including additional classes, interfaces, and value types to your class library. Also, consider adding multiple class libraries.

See Also

Other Resources

ASP.NET Web Application Projects