Configure a Web Service Client
You are creating a client application that will use a Web service to perform basic math functions. The BasicMathService Web Service is in the BasicMath namespace and has the following two methods:
- Add accepts two integer values and returns the sum as a long.
- Subtract accepts two integer values and returns the difference as a long.
You already completed the following tasks:
- Started the project.
- Created the user interface with TextBoxes for entering numbers, Labels for holding the return value, and Buttons for calling the methods.
- Added a reference to the Web Service.
In form AppMath, add Click event handlers for the Add and Subtract buttons to do the following:
- Call the corresponding method, passing the values entered in the TextBoxes.
- Place the return value in the corresponding label (lblAddResult or lblSubtractResult).
- Handle any errors if the values entered in the TextBoxes are not valid integers.
static void Main()
{
Application.Run(new AppMath());
}
#region Answer
// Add button handler
private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
// Call the Add method and return the result
BasicMath.BasicMathService basicMath = new BasicMath.BasicMathService();
lblAddResult.Text = basicMath.Add(Convert.ToInt32(tbxAddNumber1.Text), Convert.ToInt32(tbxAddNumber2.Text)).ToString();
}
// Handle errors
catch
{
lblAddResult.Text = “Invalid input values”;
}
}
// Subtract button handler
private void btnSubtract_Click(object sender, System.EventArgs e)
{
try
{
// Call the Subtract method and return the result
BasicMath.BasicMathService basicMath = new BasicMath.BasicMathService();
lblSubtractResult.Text = basicMath.Subtract(Convert.ToInt32(tbxSubtractNumber1.Text), Convert.ToInt32(tbxSubtractNumber2.Text)).ToString();
}
// Handle errors
catch
{
lblAddResult.Text = “Invalid input values”;
}
}
#endregion
}
}
Digg it | Save to del.icio.us | Netscape | Reddit | Stumble It!
- - - - - S P O N S O R I N G A D V E R T I S M E N T - - - - -