The application being built will allow a user to enter speed
and time and will calculate the distance traveled. The calculation will be
built into a class which represents the business layer of an application.
1.
Launch Visual Studio 2010. From the start page select "New
Project…"
2.
Select ASP.NET Empty Web Application from the list of templates and name
the application "TripCalculator".
3.
Click on FileàAdd New Project
from the main menu.
4.
The Add New Dialog will appear. Click on "Visual C#" from the
list of Installed Templates. Choose "Class Library" from the list of
templates. Name the project "TripCalculatorBLL" and click the OK
button.
5.
Visual Studio will add this project to your solution and create a class
file called "Class1.cs". Right click on the Class1.cs file in the
Solution Explorer and select Rename from the pop-up menu. Change the name to "DistanceCalculator.cs".
Visual Studio will ask if you want to rename all references to this new name,
click the "Yes" button.
6.
Add the following method to the DistanceCalculator class.
public double CalculateDistance(double time, double speed)
{
if (time < 0 || speed < 0)
{
throw new ArgumentOutOfRangeException();
}
else
{
return time * speed;
}
}
7.
This method will calculate the distance given the time and speed. If
the time or speed parameters are less than zero then it throws an ArgumentOutOfRangeException.
8.
Now let's add a web form that can use this class. Right click on the
TripCalcualtor project and select AddàNew
Item from the pop-up menu.
9.
Select the WebForm template and name the file Default.aspx. Click the
Add button.
10. Add
a reference to the TripCalculator project to the TripCalculatorBLL project. To
do this right click on the References in the TripCalculator project.
11. Click
Add Reference… from the pop-up menu.
12. Click
on the Projects tab from the References dialog.
13. Select
the TripCalculatorBLL project from the list and click the OK button.
14. Add
the following HTML to the web form.
<asp:Label ID="Label1" runat="server" Text="Time:"></asp:Label>
<asp:TextBox ID="txtTime" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Speed:"></asp:Label>
<asp:TextBox ID="txtSpeed" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Distance:"></asp:Label>
<asp:Label ID="lblDistance" runat="server"></asp:Label>
<br />
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" />
15. If
you switch to Design mode your form should look like the following image.
16. Double
click on the Calculate button to create the event handler.
17. Add
the following using statement to the code behind page.
18. Add
the following code to the click event handler.
try
{
DistanceCalculator distanceCalculator = new DistanceCalculator();
lblDistance.Text = distanceCalculator.CalculateDistance
(Convert.ToDouble(txtTime.Text),
Convert.ToDouble(txtSpeed.Text)).ToString();
}
catch (Exception ex)
{
lblDistance.Text = ex.Message;
}
19. Try
running the project and enter 5 for the Time and 5 for the Speed. If you click
the Calculate button you should get 25 for the Distance.