Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual C# 2005 How to Program, 2/e
Visual C# 2005 How to Program, 2/e

ISBN:
0-13-152523-9
© 2006
pages: 1535

Order
Amazon logo

[Note: This is an excerpt (Sections 22.1–22.4) of Chapter 22, Web Services, from our textbook Visual C# 2005 How to Program, 2/e. These articles may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., VISUAL C# 2005 HOW TO PROGRAM, 2/E, 2005, pp.1164–1190. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
Consuming the HugeInteger Web Service
 
The Windows Form in Fig. 22.17 uses the HugeInteger Web service to perform computations with positive integers up to 100 digits long. Line 22 declares variable remoteInteger of type localhost.HugeInteger. This variable is used in each of the application's event handlers to call methods of the HugeInteger Web service. The proxy object is created and assigned to this variable at line 31 in the Form's Load event handler. Lines 52-53, 66-67, 95-96, 116-117 and 135-136 in the various button event handlers invoke methods of the Web service. Note that each call is made on the local proxy object, which then communicates with the Web service on the client's behalf. If you downloaded the example from www.deitel.com/books/csharpforprogrammers2, you might need to regenerate the proxy by removing the Web reference, then adding it again. To do so, right click localhost in the Web References folder in the Solution Explorer and select option Delete. Then follow the instructions in the preceding section to add the Web reference to the project.
The user inputs two integers, each up to 100 digits long. Clicking a button causes the application to invoke a Web method to perform the appropriate task and return the result. Note that client application UsingHugeIntegerService cannot perform operations using 100-digit numbers directly. Instead the application creates string representations of these numbers and passes them as arguments to Web methods that handle such tasks for the client. It then uses the return value of each operation to display an appropriate message. Note that the application eliminates leading zeros in the numbers before displaying them by calling string method TrimStart. Like string method Trim (discussed in Chapter 16), TrimStart removes all occurrences of characters specified by a char array (line 24) from the beginning of a string.
Fig. 22.17 Using the HugeInteger Web service. (Part 1 of 5.)
1   // Fig. 22.17: UsingHugeIntegerService.cs
2   // Using the HugeInteger Web Service.
3   using System;
4   using System.Collections.Generic;
5   using System.ComponentModel;
6   using System.Data;
7   using System.Drawing;
8   using System.Text;
9   using System.Windows.Forms;
10   using System.Web.Services.Protocols;
11   
12   namespace UsingHugeIntegerWebService
13   {
14      public partial class UsingHugeIntegerServiceForm : Form
15      {
16         public UsingHugeIntegerServiceForm()
17         {
18            InitializeComponent();
19         } // end constructor
20   
21          // declare a reference to Web service
22         private localhost.HugeInteger remoteInteger;
23   
24         private char[] zeros = { '0' }; // character to trim from strings
25   
26         // instantiates object to interact with Web service
27         private void UsingHugeIntegerServiceForm_Load( object sender,
28            EventArgs e )
29         {
30            // instantiate remoteInteger
31            remoteInteger = new localhost.HugeInteger();
32         } // end method UsingHugeIntegerServiceForm_Load
33   
34         // adds two numbers input by user
35         private void addButton_Click( object sender, EventArgs e )
36         {
37            // make sure numbers do not exceed 100 digits and that both
38            // are not 100 digits long, which would result in overflow
39            if ( firstTextBox.Text.Length > 100 ||
40               secondTextBox.Text.Length > 100 ||
41               ( firstTextBox.Text.Length == 100 &&
42               secondTextBox.Text.Length == 100) )
43            {
44               MessageBox.Show( "HugeIntegers must not be more " +
45                  "than 100 digits\r\nBoth integers cannot be " +
46                  "of length 100: this causes an overflow", "Error",
47                  MessageBoxButtons.OK, MessageBoxIcon.Information );
48               return;
49            } // end if
50   
51            // perform addition
52            resultLabel.Text = remoteInteger.Add(
53               firstTextBox.Text, secondTextBox.Text ).TrimStart( zeros );
54         } // end method addButton_Click
55   
56         // subtracts two numbers input by user
57         private void subtractButton_Click( object sender, EventArgs e )
58         {
59             // make sure HugeIntegers do not exceed 100 digits
60            if ( SizeCheck( firstTextBox, secondTextBox ) )
61               return;
62   
63            // perform subtraction
64            try
65            {
66               string result = remoteInteger.Subtract(
67                  firstTextBox.Text, secondTextBox.Text ).TrimStart( zeros );
68   
69               if ( result == "" )
70                  resultLabel.Text = "0";
71               else
72                  resultLabel.Text = result;
73   
74            } // end try
75   
76            // if WebMethod throws an exception,
77            // then first argument was smaller than second
78            catch ( SoapException exception )
79            {
80               MessageBox.Show(
81                  "First argument was smaller than the second" );
82            } // end catch
83         } // end method subtractButton_Click
84   
85         // determines whether first number
86         // input by user is larger than second
87         private void largerButton_Click( object sender, EventArgs e )
88         {
89         // make sure HugeIntegers do not exceed 100 digits
90         if ( SizeCheck( firstTextBox, secondTextBox ) )
91            return;
92   
93         // call Web-service method to determine if
94         // first integer is larger than the second
95         if ( remoteInteger.Bigger( firstTextBox.Text,
96            secondTextBox.Text ) )
97            resultLabel.Text = firstTextBox.Text.TrimStart( zeros ) +
98               " is larger than " +
99            secondTextBox.Text.TrimStart( zeros );
100         else
101            resultLabel.Text = firstTextBox.Text.TrimStart( zeros ) +
102               " is not larger than " +
103            secondTextBox.Text.TrimStart( zeros );
104      } // end method largerButton_Click
105   
106      // determines whether first number
107      // input by user is smaller than second
108      private void smallerButton_Click( object sender, EventArgs e )
109      {
110         // make sure HugeIntegers do not exceed 100 digits
111         if ( SizeCheck( firstTextBox, secondTextBox ) )
112            return;
113   
114         // call Web-service method to determine if
115         // first integer is smaller than second
116         if ( remoteInteger.Smaller( firstTextBox.Text,
117            secondTextBox.Text ) )
118            resultLabel.Text = firstTextBox.Text.TrimStart( zeros ) +
119               " is smaller than " +
120            secondTextBox.Text.TrimStart( zeros );
121         else
122            resultLabel.Text = firstTextBox.Text.TrimStart( zeros ) +
123               " is not smaller than " +
124            secondTextBox.Text.TrimStart( zeros );
125      } // end method smallerButton_Click
126   
127      // determines whether two numbers input by user are equal
128      private void equalButton_Click( object sender, EventArgs e )
129      {
130         // make sure HugeIntegers do not exceed 100 digits
131         if ( SizeCheck( firstTextBox, secondTextBox ) )
132            return;
133   
134         // call Web-service method to determine if integers are equal
135         if ( remoteInteger.EqualTo( firstTextBox.Text,
136         secondTextBox.Text ) )
137            resultLabel.Text = firstTextBox.Text.TrimStart( zeros ) +
138               " is equal to " + secondTextBox.Text.TrimStart( zeros );
139         else
140            resultLabel.Text = firstTextBox.Text.TrimStart( zeros ) +
141               " is not equal to " +
142            secondTextBox.Text.TrimStart( zeros );
143         } // end method equalButton_Click
144   
145         // determines whether numbers input by user are too big
146         private bool SizeCheck( TextBox first, TextBox second )
147         {
148         // display an error message if either number has too many digits
149         if ( ( first.Text.Length > 100 ) ||
150            ( second.Text.Length > 100 ) )
151         {
152            MessageBox.Show( "HugeIntegers must be less than 100 digits" ,
153               "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
154            return true;
155         } // end if
156   
157         return false;
158      } // end method SizeCheck
159    } // end class UsingHugeIntegerServiceForm
160   } // end namespace UsingHugeIntegerWebService
Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

Tutorial Index