Changing Array Values in a Web Service

The previous SimpleStatistics web service used values stored in an array. The IncrementArray web service increments or decrements by a specified amount the values within the array it receives as a parameter:

integer Add(ByRef double Values(), Double Increment) integer Subtract(ByRef double Values(), Double Increment)

To create the IncrementArray web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name IncrementArray. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

        <WebMethod()> Public Function Add(ByRef Values As Double(), _ Ä   ByVal Increment As Double) As Integer         Dim Entry As Double         Dim I As Integer         For I = 0 To Values.Length - 1             Values(I) += Increment         Next         Return 0     End Function     <WebMethod()> Public Function Subtract(ByRef Values As Double(), _ Ä   ByVal Decrement As Double) As Integer         Dim Entry As Double         Dim I As Integer         For I = 0 To Values.Length - 1             Values(I) -= Decrement         Next         Return 0     End Function

  4. After you enter the code, select the Build menu Build Solution option to create the service.

To change the array values within a web service, programs must pass the arrays to the service by reference. Within each method, the code simply loops through the array elements adding or subtracting the specified amount.

Putting the IncrementArray Web Service to Use

The following C# program, UseIncrement.cs, uses the IncrementArray web service to add or subtract 100 from the values within an array. When you run the program, your screen will display a form similar to that shown in Figure 2.11 that contains the array values. When you click the Increment Values button, the program will call the service’s Add method to add the value 100 to each value in the array. Likewise, when you click the Decrement Values button, the program will use the service to subtract the value 100 from each array value.


Figure 2.11: Using a web service method to change values stored within an array

To create the UseIncrement.cs program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields type UseIncrement. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the text box and buttons previously shown in Figure 2.11 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/IncrementArray/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

          double[] Values = {1, 2, 3, 4, 5, 7, 8, 9, 10};       private void Form1_Load(object sender, System.EventArgs e)       {          foreach (double Entry in Values)             textBox1.Text += Entry + "\r\n";       }       private void button1_Click(object sender, System.EventArgs e)       {          localhost.Service1 IncObject = new localhost.Service1();                  try          {             IncObject.Add(ref Values, 100.0);             textBox1.Text = "";             foreach (double Entry in Values)                textBox1.Text += Entry + "\r\n";          }          catch (Exception Ex)          {             MessageBox.Show("Exception: " + Ex.Message);          }       }       private void button2_Click(object sender, System.EventArgs e)       {          localhost.Service1 IncObject = new localhost.Service1();                  try          {             IncObject.Subtract(ref Values, 100.0);             textBox1.Text = "";             foreach (double Entry in Values)                textBox1.Text += Entry + "\r\n";          }          catch (Exception Ex)          {             MessageBox.Show("Exception: " + Ex.Message);          }            }

The program code first defines the array of values. Then, within the Form1_Load subroutine, the program displays the values within the text box. When the user clicks the Increment Values button, the code calls the web service’s Add method to increment the values in the array. Then, the code displays the new values within the text box. Likewise, if the user clicks the Decrement Values button, the code calls the service’s Subtract method and then displays the array’s new values.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net