Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

Adding the Four-Function Calculator Code

Now that you know a bit more about what is going on in your newly created XML Web service, let's add some code and see how XML Web services actually work.

For the calculator, you should add four methods. Listing 7.4 shows the Visual Basic code that we will add. Listing 7.5 shows what one of the methods looks like in C#.

Listing 7.4 Calculator Methods in Visual Basic

1:  #Region "Calc Functions"
2:  <WebMethod()> Public Function Add(ByVal iNum1 As Integer, _
3:                  ByVal iNum2 As Integer) As Double
4:    Return iNum1 + iNum2
5:  End Function
6:
7:  <WebMethod()> Public Function Subtract(ByVal iNum1 As Integer, _
8:                  ByVal iNum2 As Integer) As Double
9:    Return iNum1 - iNum2
10:  End Function
11:
12:  <WebMethod()> Public Function Mulitply(ByVal iNum1 As Integer, _
13:                 ByVal iNum2 As Integer) As Double
14:    Return iNum1 * iNum2
15:  End Function
16:
17:  <WebMethod()> Public Function Divide(ByVal iNum1 As Integer, _
18:                 ByVal iNum2 As Integer) As Double
19:    Return iNum1 / iNum2
20:  End Function
21:  #End Region

Listing 7.5 C# Add Method for the Four Function Calculator

1:    [WebMethod]
2:    public int Add(int iNum1, int iNum2)
3:    {
4:      return iNum1 + iNum2;
5:    }

C# programmers should have no trouble at all creating the other three methods after looking at the example. Simply copy the Add() method three more times, being very careful to include the [WebMethod] tag above every method that you intend your XML Web service to expose. Then, rename each method to correspond to the three additional methods in Listing 7.4, and change the plus sign to the corresponding mathematical symbol.

Adding Descriptions to Methods

You can add an optional description to each of the methods in your service. This is accomplished by setting an optional parameter of the <WebMethod> declaration. This is its syntax in Visual Basic:

<WebMethod(Description:="Some Text")>

In C#, it looks like this:

[WebMethod (Description="Some Text")]

To add a description to your Add() method, for example, change the <WebMethod> declaration as follows:

<WebMethod(Description:="This is a function to add two integers")>

The description will show up in the services WSDL file, which we described in Hour 3, and when you run the service. Figure 7.4 shows the Add() method with the new description.

Using Regions

You may have noticed that the first line of code in Listing 7.4 contained a #Region compiler directive. For those of you who haven't discovered regions in your use of .NET, they are an extremely useful new element.

A region is a related group of code, possibly a group of functions, such as our four mathematical functions, that can be minimized by the developer. This allows you to "close down" code that you aren't currently working on and view only that which is important at the moment. This makes the code window much easier to navigate.

A region is created simply by adding the following line to your code:

#Region "Region Name"

In C#, use the following:

#region "Region Name"

"Region Name" represents any meaningful description of the region that you care to enter. After this line is added, navigate to the end of the area of code that you want to include in this region and add this line:

#End Region

In C#, use the following:

#endregion

Figure 7.2 shows the region in our four-function calculator. Notice the minus sign next to our new region. This allows us to minimize the region. The plus sign next to the line "Web Services Designer Generated Code" can be used to open up the group of functions included in our code when we first created the service.

Figure 7.2 Regions in action.

  • + Share This
  • 🔖 Save To Your Account