Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
Chapter 16
Creating a Class
Library and Using
It in an ASP.NET
Application
CREATING A CLASS LIBRARY |
Chapter 16 |
|
543 |
|
|
||||
|
|
|
|
|
tasks performed by the development team in the following stages of the projectexecution phase:
Requirements analysis
Design
Construction
Testing
Requirements Analysis
During the requirement analysis phase, the MIS team of NJ Finances analyzed the requirements of the tax calculator program and came up with the following list of requirements:
The application should accept an employee’s salary and grade, and return his or her tax liability.
The application should be integrated with the already existing modules of the intranet, which is built on ASP.NET technology.
The team already has the source code for the business logic. Currently, it is being used as part of a desktop-based application created for a similar purpose. The desktop-based application was built using C++.
Design
During this phase, the functionality to be implemented and the technology to be used are determined. Since a similar application already exists, the team decided to create a C++ class library that would contain the code reused from the desktop application. The user interface will be in ASP.NET so that it can easily be integrated with the rest of the intranet. The ASP.NET page will be created as a C# Web application.
Construction
During this phase, the team will construct the application. The primary tasks that the team needs to cater to are as follows:
Create a Managed C++ class library that will expose the functionality of calculating the tax liability.
Create an ASP.NET application in C# that will make use of this functionality.
544 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY
Testing
The application will finally be deployed on the intranet; hence, the team will test the application by invoking it from a browser. The application, currently, is in its beta stage. As a result, it has limited functionality. So, the testing would be scoped based on the functionality that the application is programmed for currently.
Creating the Managed C++ Class Library
The class library that will be created will have a single public class that will expose a single public method. The method CalculateTotalTax will accept an employee’s gross salary and grade, and return his or her tax liability. To create a Managed C++ class library, perform the following steps:
1.Start Visual Studio .NET and create a new project of the type Managed C++ Class Library as shown in Figure 16-1.
FIGURE 16-1 The Visual Studio .NET New Project dialog box
2.Name the project TaxCal and click on the OK button. The IDE creates the following files:
AssemblyInfo.cpp
TaxCalculator.cpp
CREATING A CLASS LIBRARY |
Chapter 16 |
545 |
|
|
|
|
|
StdAfx.cpp
TaxCalculator.h
StdAfx.h
These are standard files created for a Managed C++ class library application. You will learn about the role and use of these files in Part IX, Chapters 25 and 27, in which assemblies are discussed in depth.
Open the file TaxCalc.h file from the Solution Explorer window. The file has the following code:
#pragma once
using namespace System; namespace TaxCalc
{
public __gc class Class1
{
// TODO: Add your methods for this class here.
};
}
The functionality for calculating the tax liability has to be added to this class.
In any class library, there will be a set of methods that will be exposed to external clients (the ASP.NET application in this example). There will also be a set of methods that encompasses the code that handles some internal implementations. Such methods need to be available only within the class and need not be exposed to the clients. These methods are therefore declared as private methods. Typically, the methods that implement validations or some initialization-related tasks are declared as private methods.
NOTE
A Managed C++ class library is not all that different from a native C++ class library created using a different compiler for a different platform. The only difference here is that the library will use managed types, and the class library itself is housed in a
.NET DLL assembly.
546 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY
Back to the application, you need to add the following two methods to the class
Class1:
void SetTaxRate(int empType);
int CalculateTotalTax(int Salary, int Grade);
The method SetTaxRate is a private method. The CalculateTotalTax method calls this method to set the rate of interest for various grades of employees. Since this method is called only by CalculateTotalTax and does not need to be accessible to client applications, it is declared as private. CalculateTotalTax is the only method exposed to clients. You will now take a look at the complete implementation of the class:
#pragma once
using namespace System;
namespace TaxCalc
{
public __gc class Class1
{
private:
int taxrate; private:
void SetTaxRate(int emptype)
{
switch (emptype)
{
case 1: taxrate = 10; break;
case 2: taxrate = 8; break;
case 3: taxrate = 5; break;
}
}
public:
int CalculateTotalTax(int Salary, int Grade)
{
CREATING A CLASS LIBRARY |
Chapter 16 |
547 |
|
|
|
|
|
//this is to calculate the total tax int taxval;
int surcharge; int TotalTax;
//call the private method to set the tax rate SetTaxRate(Grade);
//now that we have the tax rate set
//let’s use a formula to calculate tax
taxval = (Salary * taxrate/100); // add a 2% surcharge surcharge =(taxval * 0.02);
TotalTax = taxval + surcharge; return TotalTax;
}
};
}
Now that the class is ready, you can build the class library. Since the class library is housed in an assembly, the library has to be in the same folder as the client. There is an alternative method to make assemblies available globally. You will learn about this alternative method in Chapter 26.
Creating the ASP.NET Application
To create an ASP.NET application, perform the following steps:
1.Choose File, Add Project, New Project.
2.From the Project Types box, select Visual C# Projects.
3.From the Templates list, select ASP.NET Web Application as shown in Figure 16-2.
4.Enter the name of the project as TaxCalculator and the location. The location has to be an URL.
Almost all the files, which the IDE creates for this project, will be created under a virtual directory in the URL you have specified. The virtual directory will have the same name as the project.
The new project will have the following files:
Webform1.aspx. This is the ASP.NET page that will be accessed by the browser.
548 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY
FIGURE 16-2 Adding a new ASP.NET Web Application C# project
WebForm1.cs. This file will contain any C# code that implements some business logic and that you want to keep separate from the user interface code in the ASPX file.
Config.Web. The configuration settings for the virtual directory are stored in this file. This file is in the XML format.
Global.asax. This file simply redirects access to the Global.asax.cs file that allows you to handle sessionand application-level events generated by the ASP.NET application.
Project.vsdisco. You will learn about the discovery file in Chapter 23, which deals with Web services.
You will now make the necessary modifications to the solitary ASP.NET page in the project, Webform1.aspx.
NOTE
You can rename the WebForm2.aspx file to something meaningful from the Solution Explorer. Remember that this will alter the name of the associated CS file also. Do not rename the ASPX file from Windows Explorer, because it is necessary that both the ASPX and CS files have the same name. If you rename one and leave the other as is, you will break the application.
CREATING A CLASS LIBRARY |
Chapter 16 |
|
549 |
|
|
||||
|
|
|
|
|
The following is the code snippet in the ASP.NET page, Webform1.aspx, created by the IDE:
<%@ Page language=”c#” Codebehind=”WebForm1.aspx.cs” AutoEventWireup=”false” Inherits=”TaxCalculator.WebForm1” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” > <html>
<head>
<title>WebForm1</title>
<meta name=”GENERATOR” Content=”Microsoft Visual Studio 7.0”> <meta name=”CODE_LANGUAGE” Content=”C#”>
<meta name=”vs_defaultClientScript” content=”JavaScript”> <meta name=”vs_targetSchema” content=”http://schemas.microsoft.com/intellisense/ie5”>
</head>
<body MS_POSITIONING=”GridLayout”>
<form id=”Form1” method=”post” runat=”server”> </form>
</body>
</html>
Consider the following line of code at the beginning of the ASP.NET page:
<%@ Page language=”c#” Codebehind=”WebForm1.aspx.cs” AutoEventWireup=”false”
Inherits=”TaxCalculator.WebForm1” %>
This line indicates that the code for implementing the business logic and functionality of the page is in the C# file, WebForm1.aspx.cs. The rest of the page contains the code for a generic ASP.NET page.
Design the UI of the ASP program as shown in Figure 16-3.
You can drag and drop controls from the toolbox into the Web form:
Add a TextBox control to the Web form. The user will use this control to enter his or her total salary.
Right-click on the control and select Properties. In the Properties dialog box, assign a suitable name to the control.
Add a DropDownList control to the form and initialize it with the values:
Manager
Executive
