Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

550 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

 

Y

L

F

M

 

A

 

FIGURE 16-3 The UI of the SP.NET application

 

EJunior staffT

The user will select his/her grade from this list. Give a suitable name to the control.

Add another Textbox control. This control will be used to display the salary after the tax deduction. Give a suitable name to the control.

Add a RequiredFieldValidator control to the form.

Invoke the Properties dialog box of the validator control and set the following properties:

Control to Validate property. Set to the name of the first text box

ErrorMessage property. Set to the message “Error: Please enter a valid salary”

Add a Button control to the form and set its Text property to Calculate

Tax.

The ASP code for the page, which is created automatically by the editor, is shown here:

<%@ Page language=”c#” Codebehind=”WebForm1.aspx.cs” AutoEventWireup=”false”

Inherits=”TaxCalculator.WebForm1” %>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” >

<HTML>

Team-Fly®

CREATING A CLASS LIBRARY

Chapter 16

551

 

 

 

 

<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”>

<asp:Button id=”Button1” style=”Z-INDEX: 101; LEFT: 73px; POSITION: absolute; TOP: 206px” runat=”server” Text=”Calculate TAX” Width=”122px” Height=”36px” Font-Bold=”True”></asp:Button>

<asp:Label id=”Label1” style=”Z-INDEX: 104; LEFT: 13px; POSITION: absolute; TOP: 62px” runat=”server” Width=”92px” Height=”27px” Font-Bold=”True”>Total Salary:</asp:Label>

<asp:TextBox id=”TextBox1” style=”Z-INDEX: 102; LEFT: 135px; POSITION: absolute; TOP: 61px” runat=”server” Width=”129px” Height=”23px” AutoPostBack=”True”></asp:TextBox>

<asp:DropDownList id=”DropDownList1” style=”Z-INDEX: 103; LEFT: 136px; POSITION: absolute; TOP: 117px” runat=”server” Width=”127px” Height=”27px”>

<asp:ListItem Value=”1”>Manager</asp:ListItem> <asp:ListItem Value=”2”>Executives</asp:ListItem> <asp:ListItem Value=”3”>Junior Staff</asp:ListItem> </asp:DropDownList>

<asp:Label id=”Label2” style=”Z-INDEX: 105; LEFT: 27px; POSITION: absolute; TOP: 263px” runat=”server” Width=”280px” Height=”69px” Font-Bold=”True” Font-Names=”Arial Black”></asp:Label>

<asp:RequiredFieldValidator id=”RequiredFieldValidator1” style=”Z-INDEX: 106; LEFT: 142px; POSITION: absolute; TOP: 90px” runat=”server” ErrorMessage=”ERROR: Please enter a valid salary” ControlToValidate=”TextBox1”></asp:RequiredFieldValidator>

<asp:Label id=”Label3” style=”Z-INDEX: 107; LEFT: 14px; POSITION: absolute; TOP: 116px” runat=”server” Width=”108px” Height=”37px” FontBold=”True”>Choose Grade:</asp:Label>

<asp:Label id=”Label4” style=”Z-INDEX: 108; LEFT: 40px; POSITION: absolute; TOP: 5px” runat=”server” Font-Bold=”True” Height=”32px” Width=”226px” Font-Names=”Arial Black” BackColor=”#C0C0FF” BorderWidth=”3px”>Simple Tax

552 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

Calculator</asp:Label>

<asp:Label id=”Label5” style=”Z-INDEX: 109; LEFT: 14px; POSITION: absolute; TOP: 161px” runat=”server” Font-Bold=”True” Height=”23px” Width=”119px”>Take Home Salary:</asp:Label>

<asp:TextBox id=”TextBox2” style=”Z-INDEX: 110; LEFT: 136px; POSITION: absolute; TOP: 160px” runat=”server” Height=”21px” Width=”116px” ReadOnly=”True”></asp:TextBox>

</form>

</body>

</HTML>

Next, you need to add a reference to the class library object created previously to the current project. To do that, right-click on the project name in the Solution Explorer window and select Add Reference in the resulting menu. This will open the Add Reference dialog box, displayed in Figure 16-4.

FIGURE 16-4 The Add Reference dialog box

Click on the Browse button. Select the TaxCalc.dll that you created earlier. Once the reference is added, you can set a reference to the TaxCalc class library in the References section of the Solution Explorer.

CREATING A CLASS LIBRARY

Chapter 16

553

 

 

 

 

All that is now left for you to do is to use the functionality of the class library in your ASP.NET application. In the Web form, double-click on the Calculate Tax button. This will open the code for the event handler for the Click event in the editor. Add the following code to the event handler:

private void Button1_Click(object sender, System.EventArgs e)

{

int taxv; int sal; int grd;

sal = Convert.ToInt32(TextBox1.Text);

grd = Convert.ToInt32(DropDownList1.SelectedItem.Value); TaxCalculator.Class1 obj1 = new TaxCalculator.Class1(); taxv = obj1.CalculateTotalTax(sal,grd);

TextBox2.Text = Convert.ToString(sal - taxv);

Label2.Text = “Note: Your Total Tax liability is Rs. “ + taxv;

}

ASP.NET applications need to have a page designated as the start page. This is the page that will be opened when a browser connects to the virtual directory containing the ASP.NET application. To do this, right-click the ASPX file in the project and select Set As Start Page.

Now you can build the application. Once built, the application can be tested by connecting to the virtual directory where you initially stored your ASP.NET application.

NOTE

As part of this chapter, you were introduced to Visual C#.NET — a new addition to the family of languages. If you are interested in knowing more about Visual C#.NET, Appendix E gives you an overview of Visual C#.

554 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

Summary

In this chapter, you created a simple Managed C++ class library and used the class library to house business logic for an ASP.NET application created using C#. This chapter showed how you can leverage existing business logic libraries written in C++ for .NET Web applications.

PARTVIProfessional

Project – 5

This page intentionally left blank

Project 5

Creating a COM

Component

Using ATL

Project 5 Overview

In this project, you will create a COM component using ATL. This component will validate a credit card number using the Luhn algorithm.

In a typical online credit card transaction, the following participants are involved:

The issuer. A financial institution such as a bank that issues credit cards.

The merchant. The site that sells goods and accepts online payment.

The acquirer. Typically a bank with which the merchant has an account and that authorizes payments and processes payments.

The buyer. The person who makes a purchase and pays for it using a credit card.

The gateway. This is normally a third-party service provider, which acts as the link between the online site and the acquirer’s software system. This it does by providing an interface in the form of a component that exposes an API, which provides services like credit card validation and payment processing. The online site will be charged on a per-transaction basis.

The COM component you will create, can be used to verify a credit card number, before further validating it with a gateway service provider. Most credit card issuers use an algorithm called the Luhn algorithm to generate the card numbers. The component you will create will simply check the given number against this algorithm. This is not meant to actually validate the credit card for payment processing. You can use this component to check if the visitor has made a mistake while typing in a number or has entered a spurious number, without having to connect to a service provider.

In this project, I will show how ATL and attributes make creating COM components easy. Also, you will learn how to handle different COM data types using functions and macros that are a part of ATL.

The concepts that I’ll use to build the credit card validation component are the following:

COM

ATL

The component created will be tested by accessing it from clients built using Visual Basic and ASP.

Chapter 17

Introduction to COM