Presentation is loading. Please wait.

Presentation is loading. Please wait.

2002 Prentice Hall. All rights reserved. 1 Chapter 21 – ASP.NET and Web Services Outline 21.1 Introduction 21.2 Web Services 21.3 Simple Object Access.

Similar presentations


Presentation on theme: "2002 Prentice Hall. All rights reserved. 1 Chapter 21 – ASP.NET and Web Services Outline 21.1 Introduction 21.2 Web Services 21.3 Simple Object Access."— Presentation transcript:

1 2002 Prentice Hall. All rights reserved. 1 Chapter 21 – ASP.NET and Web Services Outline 21.1 Introduction 21.2 Web Services 21.3 Simple Object Access Protocol (SOAP) and Web Services 21.4 Publishing and Consuming Web Services 21.5 Session Tracking in Web Services 21.6 Using Web Forms and Web Services 21.7 Case Study: Temperature Information Application 21.8 User-Defined Types in Web Services

2 2002 Prentice Hall. All rights reserved Web Services Fig. 21.1ASMX file rendered in Internet Explorer. Link to service description Links to web service methods

3 2002 Prentice Hall. All rights reserved Web Services Fig. 21.2Service description for a Web service.

4 2002 Prentice Hall. All rights reserved Web Services Fig. 21.3Invoking a method of a Web service from a Web browser.

5 2002 Prentice Hall. All rights reserved Web Services Fig. 21.4Results of invoking a Web-service method from a Web browser.

6 2002 Prentice Hall. All rights reserved Simple Object Access Protocol (SOAP) and Web Services Fig. 21.5SOAP request for the HugeInteger Web service. Name of method Represent arguments named first and second. The value string indicates that they need to be of type string, but in a method call they would be replaced with the argument values.

7 2002 Prentice Hall. All rights reserved. Outline 7 HugeInteger.asmx.cs 1 // Fig. 21.6: HugeInteger.asmx.cs 2 // HugeInteger Web Service. 3 4 using System; 5 using System.Text; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Data; 9 using System.Diagnostics; 10 using System.Web; 11 using System.Web.Services; // contains Web service related classes namespace HugeIntegerWebService 14 { 15 /// 16 /// performs operations on large integers 17 /// 18 [ WebService( 19 Namespace = "http://www.deitel.com/csphtp1/ch21/", 20 Description = "A Web service which provides methods that" + 21 " can manipulate large integer values." ) ] 22 public class HugeInteger : System.Web.Services.WebService 23 { 24 // default constructor 25 public HugeInteger() 26 { 27 // CODEGEN: This call is required by the ASP.NET Web 28 // Services Designer 29 InitializeComponent(); number = new int[ MAXIMUM ]; 32 } 33 Set Namespace property of WebService attribute to specify the namespace that the Web service belongs to Set Description property of WebService attribute to describe the function of the Web Service Class HugeInteger inherits from System.Web.Services.WebService

8 2002 Prentice Hall. All rights reserved. Outline 8 HugeInteger.asmx.cs 34 #region Component Designer generated code 35 /// 36 /// Required method for Designer support - do not modify 37 /// the contents of this method with the code editor. 38 /// 39 private void InitializeComponent() 40 { 41 } 42 #endregion /// 45 /// Clean up any resources being used. 46 /// 47 protected override void Dispose( bool disposing ) 48 { 49 } // WEB SERVICE EXAMPLE 52 // The HelloWorld() example service returns 53 // the string Hello World 54 // To build, uncomment the following lines 55 // then save and build the project 56 // To test this web service, press F // [WebMethod] 59 // public string HelloWorld() 60 // { 61 // return "Hello World"; 62 // } private const int MAXIMUM = 100; public int[] number; 67

9 2002 Prentice Hall. All rights reserved. Outline 9 HugeInteger.asmx.cs 68 // indexer that accepts an integer parameter 69 public int this[ int index ] 70 { 71 get 72 { 73 return number[ index ]; 74 } set 77 { 78 number[ index ] = value; 79 } } // end indexer // returns string representation of HugeInteger 84 public override string ToString() 85 { 86 StringBuilder returnString = new StringBuilder(); foreach ( int digit in number ) 89 returnString.Insert( 0, digit ); return returnString.ToString(); 92 } // creates HugeInteger based on argument 95 public static HugeInteger FromString( string integer ) 96 { 97 HugeInteger parsedInteger = new HugeInteger(); for ( int i = 0; i < integer.Length; i++ ) 100 parsedInteger[ i ] = Int32.Parse( 101 integer[ integer.Length - i - 1 ].ToString() ); Indexer for class HugeInteger

10 2002 Prentice Hall. All rights reserved. Outline 10 HugeInteger.asmx.cs return parsedInteger; 104 } // WebMethod that performs integer addition 107 // represented by string arguments 108 [ WebMethod ( Description = "Adds two huge integers." ) ] 109 public string Add( string first, string second ) 110 { 111 int carry = 0; HugeInteger operand1 = HugeInteger.FromString( first ); 114 HugeInteger operand2 = 115 HugeInteger.FromString( second ); // store result of addition 118 HugeInteger result = new HugeInteger(); // perform addition algorithm for each digit 121 for ( int i = 0; i < MAXIMUM; i++ ) 122 { 123 // add two digits in same column 124 // result is their sum, plus carry from 125 // previous operation modulus result[ i ] = 127 ( operand1[ i ] + operand2[ i ] ) % 10 + carry; // store remainder of dividing 130 // sums of two digits by carry = ( operand1[ i ] + operand2[ i ] ) / 10; 132 } return result.ToString(); } // end method Add Method Add that adds two HugeInteger s WebMethod attribute specifies that this method may be called by remote client applications Description property of WebMethod attribute summarizes the function of the method

11 2002 Prentice Hall. All rights reserved. Outline 11 HugeInteger.asmx.cs // WebMethod that performs the subtraction of integers 139 // represented by string arguments 140 [ WebMethod ( 141 Description = "Subtracts two huge integers." ) ] 142 public string Subtract( string first, string second ) 143 { 144 HugeInteger operand1 = HugeInteger.FromString( first ); 145 HugeInteger operand2 = 146 HugeInteger.FromString( second ); 147 HugeInteger result = new HugeInteger(); // subtract top digit from bottom digit 150 for ( int i = 0; i < MAXIMUM; i++ ) 151 { 152 // if top digit is smaller than bottom 153 // digit we need to borrow 154 if ( operand1[ i ] < operand2[ i ] ) 155 Borrow( operand1, i ); // subtract bottom from top 158 result[ i ] = operand1[ i ] - operand2[ i ]; 159 } return result.ToString(); } // end method Subtract // borrows 1 from next digit 166 private void Borrow( HugeInteger integer, int place ) 167 { 168 // if no place to borrow from, signal problem 169 if ( place >= MAXIMUM - 1 ) 170 throw new ArgumentException(); 171 Method Subtract that subtracts one HugeInteger from another Helper method Borrow used by method Subtract when 1 needs to be borrowed from the next number

12 2002 Prentice Hall. All rights reserved. Outline 12 HugeInteger.asmx.cs 172 // otherwise if next digit is zero, 173 // borrow from digit to left 174 else if ( integer[ place + 1 ] == 0 ) 175 Borrow( integer, place + 1 ); // add ten to current place because we borrowed 178 // and subtract one from previous digit // this is digit borrowed from 180 integer[ place ] += 10; 181 integer[ place + 1 ] -= 1; } // end method Borrow // WebMethod that returns true if first integer is 186 // bigger than second 187 [ WebMethod ( Description = "Determines whether first " "integer is larger than the second integer." ) ] 189 public bool Bigger( string first, string second ) 190 { 191 char[] zeroes = { '0' }; try 194 { 195 // if elimination of all zeroes from result 196 // of subtraction is an empty string, 197 // numbers are equal, so return false, 198 // otherwise return true 199 if ( Subtract( first, second ).Trim( zeroes ) == "" ) 200 return false; 201 else 202 return true; 203 } 204 Returns true if the first integer is bigger then the second and false otherwise

13 2002 Prentice Hall. All rights reserved. Outline 13 HugeInteger.asmx.cs 205 // if ArgumentException occurs, first number 206 // was smaller, so return false 207 catch ( ArgumentException ) 208 { 209 return false; 210 } } // end method Bigger // WebMethod returns true if first integer is 215 // smaller than second 216 [ WebMethod ( Description = "Determines whether the " "first integer is smaller than the second integer." ) ] 218 public bool Smaller( string first, string second ) 219 { 220 // if second is bigger than first, then first is 221 // smaller than second 222 return Bigger( second, first ); 223 } // WebMethod that returns true if two integers are equal 226 [ WebMethod ( Description = "Determines whether the " "first integer is equal to the second integer." ) ] 228 public bool EqualTo( string first, string second ) 229 { 230 // if either first is bigger than second, or first is 231 // smaller than second, they are not equal 232 if ( Bigger( first, second ) || 233 Smaller( first, second ) ) 234 return false; Returns true if the first integer is smaller then the second and false otherwise Returns true if the two integers are equal and false otherwise

14 2002 Prentice Hall. All rights reserved. Outline 14 HugeInteger.asmx.cs Program Output 235 else 236 return true; 237 } } // end class HugeInteger } // end namespace HugeIntegerWebService

15 2002 Prentice Hall. All rights reserved Publishing and Consuming Web Services Fig. 21.7Design view of a Web service.

16 2002 Prentice Hall. All rights reserved Publishing and Consuming Web Services Fig. 21.8Adding a Web service reference to a project.

17 2002 Prentice Hall. All rights reserved Publishing and Consuming Web Services Fig Add Web Reference dialog. Link to root directory of web server

18 2002 Prentice Hall. All rights reserved Publishing and Consuming Web Services Fig Web services located on localhost.

19 2002 Prentice Hall. All rights reserved Publishing and Consuming Web Services Fig Web reference selection and description.

20 2002 Prentice Hall. All rights reserved Publishing and Consuming Web Services Fig Solution Explorer after adding a Web reference to a project. Service description Proxy class Web service discovery file

21 2002 Prentice Hall. All rights reserved. Outline 21 UsingHugeInteger Service.cs 1 // Fig : UsingHugeIntegerService.cs 2 // Using the HugeInteger Web Service. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Web.Services.Protocols; // allows user to perform operations on large integers 12 public class UsingHugeIntService : System.Windows.Forms.Form 13 { 14 private System.Windows.Forms.Label promptLabel; 15 private System.Windows.Forms.Label resultLabel; private System.Windows.Forms.TextBox firstTextBox; 18 private System.Windows.Forms.TextBox secondTextBox; private System.Windows.Forms.Button addButton; 21 private System.Windows.Forms.Button subtractButton; 22 private System.Windows.Forms.Button biggerButton; 23 private System.Windows.Forms.Button smallerButton; 24 private System.Windows.Forms.Button equalButton; private System.ComponentModel.Container components = null; // declare a reference Web service 29 private localhost.HugeInteger remoteInteger; private char[] zeroes = { '0' }; 32

22 2002 Prentice Hall. All rights reserved. Outline 22 UsingHugeInteger Service.cs 33 // default constructor 34 public UsingHugeIntService() 35 { 36 InitializeComponent(); // instantiate remoteInteger 39 remoteInteger = new localhost.HugeInteger(); 40 } // Visual Studio.NET generated code [STAThread] 45 static void Main() 46 { 47 Application.Run( new UsingHugeIntService() ); } // end Main // checks whether two numbers user input are equal 52 protected void equalButton_Click( 53 object sender, System.EventArgs e ) 54 { 55 // make sure HugeIntegers do not exceed 100 digits 56 if ( CheckSize( firstTextBox, secondTextBox ) ) 57 return; // call Web-service method to determine 60 // whether integers are equal 61 if ( remoteInteger.EqualTo( 62 firstTextBox.Text, secondTextBox.Text ) ) resultLabel.Text = 65 firstTextBox.Text.TrimStart( zeroes ) + 66 " is equal to " + 67 secondTextBox.Text.TrimStart( zeroes ); Instantiate remoteInteger to be a new HugeInteger object Call HugeInteger Web method EqualTo remotely

23 2002 Prentice Hall. All rights reserved. Outline 23 UsingHugeInteger Service.cs 68 else 69 resultLabel.Text = 70 firstTextBox.Text.TrimStart( zeroes ) + 71 " is NOT equal to " + 72 secondTextBox.Text.TrimStart( zeroes ); } // end method equalButton_Click // checks whether first integer input 77 // by user is smaller than second 78 protected void smallerButton_Click( 79 object sender, System.EventArgs e ) 80 { 81 // make sure HugeIntegers do not exceed 100 digits 82 if ( CheckSize( firstTextBox, secondTextBox ) ) 83 return; // call Web-service method to determine whether first 86 // integer is smaller than second 87 if ( remoteInteger.Smaller( 88 firstTextBox.Text, secondTextBox.Text ) ) resultLabel.Text = 91 firstTextBox.Text.TrimStart( zeroes ) + 92 " is smaller than " + 93 secondTextBox.Text.TrimStart( zeroes ); 94 else 95 resultLabel.Text = 96 firstTextBox.Text.TrimStart( zeroes ) + 97 " is NOT smaller than " + 98 secondTextBox.Text.TrimStart( zeroes ); } // end method smallerButton_Click 101 Call HugeInteger Web method Smaller remotely

24 2002 Prentice Hall. All rights reserved. Outline 24 UsingHugeInteger Service.cs 102 // checks whether first integer input 103 // by user is bigger than second 104 protected void biggerButton_Click( 105 object sender, System.EventArgs e ) 106 { 107 // make sure HugeIntegers do not exceed 100 digits 108 if ( CheckSize( firstTextBox, secondTextBox ) ) 109 return; // call Web-service method to determine whether first 112 // integer is larger than the second 113 if ( remoteInteger.Bigger( firstTextBox.Text, 114 secondTextBox.Text ) ) resultLabel.Text = 117 firstTextBox.Text.TrimStart( zeroes ) " is larger than " secondTextBox.Text.TrimStart( zeroes ); 120 else 121 resultLabel.Text = 122 firstTextBox.Text.TrimStart( zeroes ) " is NOT larger than " secondTextBox.Text.TrimStart( zeroes ); } // end method biggerButton_Click // subtract second integer from first 129 protected void subtractButton_Click( 130 object sender, System.EventArgs e ) 131 { 132 // make sure HugeIntegers do not exceed 100 digits 133 if ( CheckSize( firstTextBox, secondTextBox ) ) 134 return; 135 Call HugeInteger Web method Bigger remotely

25 2002 Prentice Hall. All rights reserved. Outline 25 UsingHugeInteger Service.cs 136 // perform subtraction 137 try 138 { 139 string result = remoteInteger.Subtract( 140 firstTextBox.Text, 141 secondTextBox.Text ).TrimStart( zeroes ); resultLabel.Text = ( ( result == "" ) ? "0" : result ); 144 } // if WebMethod throws an exception, then first 147 // argument was smaller than second 148 catch ( SoapException ) 149 { 150 MessageBox.Show( 151 "First argument was smaller than the second" ); 152 } } // end method subtractButton_Click // adds two integers input by user 157 protected void addButton_Click( 158 object sender, System.EventArgs e ) 159 { 160 // make sure HugeInteger does not exceed 100 digits 161 // and is not situation where both integers are // digits long--result in overflow 163 if ( firstTextBox.Text.Length > 100 || 164 secondTextBox.Text.Length > 100 || 165 ( firstTextBox.Text.Length == 100 && 166 secondTextBox.Text.Length == 100 ) ) 167 { Call HugeInteger Web method Subtract remotely

26 2002 Prentice Hall. All rights reserved. Outline 26 UsingHugeInteger Service.cs 168 MessageBox.Show( "HugeIntegers must not be more " "than 100 digits\nBoth integers cannot be of" " length 100: this causes an overflow", 171 "Error", MessageBoxButtons.OK, 172 MessageBoxIcon.Information ); return; 175 } // perform addition 178 resultLabel.Text = remoteInteger.Add( firstTextBox.Text, 179 secondTextBox.Text ).TrimStart( zeroes ).ToString(); } // end method addButton_Click // determines whether size of integers is too big 184 private bool CheckSize( TextBox first, TextBox second ) 185 { 186 if ( first.Text.Length > 100 || second.Text.Length > 100 ) 187 { 188 MessageBox.Show( "HugeIntegers must be less than 100" " digits", "Error", MessageBoxButtons.OK, 190 MessageBoxIcon.Information ); return true; 193 } return false; } // end method CheckSize } // end class UsingHugeIntegerService Call HugeInteger Web method Add remotely

27 2002 Prentice Hall. All rights reserved. Outline 27 UsingHugeInteger Service.cs Program Output

28 2002 Prentice Hall. All rights reserved. Outline 28 BlackjackService.asmx.cs 1 // Fig : BlackjackService.asmx.cs 2 // Blackjack Web Service which manipulates a deck of cards. 3 4 using System; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Data; 8 using System.Diagnostics; 9 using System.Web; 10 using System.Web.Services; namespace BlackjackWebService 13 { 14 [ WebService( 15 Namespace = "http://www.deitel.com/csphtp1/ch21/", 16 Description = "A Web service that provides methods " + 17 "to manipulate a deck of cards." ) ] 18 public class BlackjackService : System.Web.Services.WebService 19 { // Visual Studio.NET generated code // deal new card 24 [ WebMethod( EnableSession = true, 25 Description = "Deal a new card from the deck." ) ] 26 public string DealCard() 27 { 28 string card = "2 2"; 29 Method DealCard with WebMethod attribute which allows client applications to invoke it Property EnableSession is set to true allowing this method to maintain session information

29 2002 Prentice Hall. All rights reserved. Outline 29 BlackjackService.asmx.cs 30 // get client's deck 31 ArrayList deck = ( ArrayList ) Session[ "deck" ]; 32 card = ( string ) deck[ 0 ]; 33 deck.RemoveAt( 0 ); 34 return card; } // end method DealCard [ WebMethod( EnableSession = true, 39 Description = "Create and shuffle a deck of cards." ) ] 40 public void Shuffle() 41 { 42 Random randomObject = new Random(); ArrayList deck = new ArrayList(); // generate all possible cards 47 for ( int i = 2; i < 15; i++ ) { 48 for ( int j = 0; j < 4; j++ ) { 49 deck.Add( i + " " + j ); 50 } 51 } // swap each card with another card randomly 54 for ( int i = 0; i < deck.Count; i++ ) 55 { 56 int newIndex = randomObject.Next( deck.Count ); 57 object temporary = deck[ i ]; 58 deck[ i ] = deck[ newIndex ]; 59 deck[ newIndex ] = temporary; 60 } // add this deck to user's session state 63 Session[ "deck" ] = deck; 64 } Retrieve the deck variable from the Session object. Cast the resulting object to an ArrayList and assign local variable deck to reference it Set card to be the first object in deck (cast to a string ) Remove the first object from deck Initialize deck to a new ArrayList object Generate all possible cards and add them to deck Swap each card with a random card from the deck Assign the Session variable deck to reference ArrayList deck

30 2002 Prentice Hall. All rights reserved. Outline 30 BlackjackService.asmx.cs // computes value of hand 67 [ WebMethod ( Description = "Compute a " + 68 "numerical value for the current hand." ) ] 69 public int CountCards( string dealt ) 70 { 71 // split string containing cards 72 char[] tab = { '\t' }; 73 string[] cards = dealt.Split( tab ); 74 int total = 0, face, aceCount = 0; foreach ( string drawn in cards ) 77 { 78 // get face of card 79 face = 80 Int32.Parse( drawn.Substring( 81 0, drawn.IndexOf( " " ) ) ); switch ( face ) 84 { 85 // if ace, increment number of aces in hand 86 case 14: 87 aceCount++; 88 break; // if Jack, Queen or King, add 10 to total 91 case 11: case 12: case 13: 92 total += 10; 93 break; 94 Use method Split to create a string array containing the individual cards (stored in dealt and separated by tab characters) Repeat loop for each member of cards If the card is in Ace, increment aceCount but do not add to total Add 10 to total if the card is a Jack, Queen or King

31 2002 Prentice Hall. All rights reserved. Outline 31 BlackjackService.asmx.cs 95 // otherwise, add value of face 96 default: 97 total += face; 98 break; } // end switch } // end foreach // if any aces, calculate optimum total 105 if ( aceCount > 0 ) 106 { 107 // if it is possible to count one ace as 11, and rest 108 // 1 each, do so; otherwise, count all aces as 1 each 109 if ( total aceCount - 1 <= 21 ) 110 total += 11 + aceCount - 1; 111 else 112 total += aceCount; 113 } return total; } // end method CountCards } // end class BlackjackService } // end namespace BlackjackWebService Otherwise, add the value of the card to total If it is possible to count one ace as 11, do so; otherwise, count each ace as 1

32 2002 Prentice Hall. All rights reserved. Outline 32 Blackjack.cs 1 // Fig : Blackjack.cs 2 // Blackjack game that uses the Blackjack Web service. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 using System.Net; // game that uses Blackjack Web Service 13 public class Blackjack : System.Windows.Forms.Form 14 { 15 private System.Windows.Forms.PictureBox pictureBox1; 16 private System.Windows.Forms.PictureBox pictureBox2; 17 private System.Windows.Forms.PictureBox pictureBox3; 18 private System.Windows.Forms.PictureBox pictureBox4; 19 private System.Windows.Forms.PictureBox pictureBox5; 20 private System.Windows.Forms.PictureBox pictureBox6; 21 private System.Windows.Forms.PictureBox pictureBox7; 22 private System.Windows.Forms.PictureBox pictureBox8; 23 private System.Windows.Forms.PictureBox pictureBox9; 24 private System.Windows.Forms.PictureBox pictureBox10; 25 private System.Windows.Forms.PictureBox pictureBox11; 26 private System.Windows.Forms.PictureBox pictureBox12; 27 private System.Windows.Forms.PictureBox pictureBox13; 28 private System.Windows.Forms.PictureBox pictureBox14; 29 private System.Windows.Forms.PictureBox pictureBox15; 30 private System.Windows.Forms.PictureBox pictureBox16; 31 private System.Windows.Forms.PictureBox pictureBox17; 32 private System.Windows.Forms.PictureBox pictureBox18; 33 private System.Windows.Forms.PictureBox pictureBox19; 34 private System.Windows.Forms.PictureBox pictureBox20; 35 private System.Windows.Forms.PictureBox pictureBox21;

33 2002 Prentice Hall. All rights reserved. Outline 33 Blackjack.cs 36 private System.Windows.Forms.PictureBox pictureBox22; private System.Windows.Forms.Button dealButton; 39 private System.Windows.Forms.Button hitButton; 40 private System.Windows.Forms.Button stayButton; private System.ComponentModel.Container components = null; private localhost.BlackjackService dealer; 45 private string dealersCards, playersCards; 46 private ArrayList cardBoxes; 47 private int playerCard, dealerCard; // labels displaying game status, dealer and player 50 private System.Windows.Forms.Label dealerLabel; 51 private System.Windows.Forms.Label playerLabel; 52 private System.Windows.Forms.Label statusLabel; public enum GameStatus : 55 int { PUSH, LOSE, WIN, BLACKJACK }; public Blackjack() 58 { 59 InitializeComponent(); dealer = new localhost.BlackjackService(); // allow session state 64 dealer.CookieContainer = new CookieContainer(); cardBoxes = new ArrayList(); // put PictureBoxes into cardBoxes 69 cardBoxes.Add( pictureBox1 ); 70 cardBoxes.Add( pictureBox2 ); Instantiate a new BlackjackService object Instantiate dealer s CookieContainer property to allow the Web service to maintain session state information

34 2002 Prentice Hall. All rights reserved. Outline 34 Blackjack.cs 71 cardBoxes.Add( pictureBox3 ); 72 cardBoxes.Add( pictureBox4 ); 73 cardBoxes.Add( pictureBox5 ); 74 cardBoxes.Add( pictureBox6 ); 75 cardBoxes.Add( pictureBox7 ); 76 cardBoxes.Add( pictureBox8 ); 77 cardBoxes.Add( pictureBox9 ); 78 cardBoxes.Add( pictureBox10 ); 79 cardBoxes.Add( pictureBox11 ); 80 cardBoxes.Add( pictureBox12 ); 81 cardBoxes.Add( pictureBox13 ); 82 cardBoxes.Add( pictureBox14 ); 83 cardBoxes.Add( pictureBox15 ); 84 cardBoxes.Add( pictureBox16 ); 85 cardBoxes.Add( pictureBox17 ); 86 cardBoxes.Add( pictureBox18 ); 87 cardBoxes.Add( pictureBox19 ); 88 cardBoxes.Add( pictureBox20 ); 89 cardBoxes.Add( pictureBox21 ); 90 cardBoxes.Add( pictureBox22 ); } // end method Blackjack // Visual Studio.NET generated code [STAThread] 97 static void Main() 98 { 99 Application.Run( new Blackjack() ); } // end Main 102

35 2002 Prentice Hall. All rights reserved. Outline 35 Blackjack.cs 103 // deals cards to dealer while dealer's total is 104 // less than 17, then computes value of each hand 105 // and determines winner 106 protected void stayButton_Click( 107 object sender, System.EventArgs e ) 108 { 109 stayButton.Enabled = false; 110 hitButton.Enabled = false; 111 dealButton.Enabled = true; 112 DealerPlay(); 113 } // process dealers turn 116 private void DealerPlay() 117 { 118 // while value of dealer's hand is below 17, 119 // dealer must take cards 120 while ( dealer.CountCards( dealersCards ) < 17 ) 121 { 122 dealersCards += "\t" + dealer.DealCard(); 123 DisplayCard( dealerCard, "" ); 124 dealerCard++; 125 MessageBox.Show( "Dealer takes a card" ); 126 } int dealersTotal = dealer.CountCards( dealersCards ); 129 int playersTotal = dealer.CountCards( playersCards ); // if dealer busted, player wins 132 if ( dealersTotal > 21 ) 133 { 134 GameOver( GameStatus.WIN ); 135 return; 136 } 137 Call method DealerPlay to process the dealers turn when the user wishes to stay While the dealers hand totals less then 17, the dealer must take cards If dealer went over 21, players wins

36 2002 Prentice Hall. All rights reserved. Outline 36 Blackjack.cs 138 // if dealer and player have not exceeded 21, 139 // higher score wins; equal scores is a push. 140 if ( dealersTotal > playersTotal ) 141 GameOver( GameStatus.LOSE ); 142 else if ( playersTotal > dealersTotal ) 143 GameOver( GameStatus.WIN ); 144 else 145 GameOver( GameStatus.PUSH ); } // end method DealerPlay // deal another card to player 150 protected void hitButton_Click( 151 object sender, System.EventArgs e ) 152 { 153 // get player another card 154 string card = dealer.DealCard(); 155 playersCards += "\t" + card; 156 DisplayCard( playerCard, card ); 157 playerCard++; int total = dealer.CountCards( playersCards ); // if player exceeds 21, house wins 162 if ( total > 21 ) 163 GameOver( GameStatus.LOSE ); 164 If neither the dealer nor the player have went over 21, higher score wins; equal scores result in a push Deal the player a card as requestedIf player went over 21, the house wins

37 2002 Prentice Hall. All rights reserved. Outline 37 Blackjack.cs 165 // if player has 21, they cannot take more cards 166 // the dealer plays 167 if ( total == 21 ) 168 { 169 hitButton.Enabled = false; 170 DealerPlay(); 171 } } // end method hitButton_Click // deal two cards each to dealer and player 176 protected void dealButton_Click( 177 object sender, System.EventArgs e ) 178 { 179 string card; // clear card images 182 foreach ( PictureBox cardImage in cardBoxes ) 183 cardImage.Image = null; // clear status from previous game 186 statusLabel.Text = ""; // shuffle cards 189 dealer.Shuffle(); // deal two cards to player 192 playersCards = dealer.DealCard(); 193 DisplayCard( 11, playersCards ); 194 card = dealer.DealCard(); 195 DisplayCard( 12, card ); 196 playersCards += "\t" + card; 197 If the player has 21, the player cannot take more cards; dealers turn Clear the PictureBox es containing the card images Shuffle the cards by calling BlackjackService method Shuffle Deal two cards to player

38 2002 Prentice Hall. All rights reserved. Outline 38 Blackjack.cs 198 // deal two cards to dealer, only display face 199 // of first card 200 dealersCards = dealer.DealCard() ; 201 DisplayCard( 0, dealersCards ); 202 card = dealer.DealCard(); 203 DisplayCard( 1, "" ); 204 dealersCards += "\t" + card; stayButton.Enabled = true; 207 hitButton.Enabled = true; 208 dealButton.Enabled = false; int dealersTotal = dealer.CountCards( dealersCards ); 211 int playersTotal = dealer.CountCards( playersCards ); // if hands equal 21, it is a push 214 if ( dealersTotal == playersTotal && 215 dealersTotal == 21 ) 216 GameOver( GameStatus.PUSH ); // if player has 21 player wins with blackjack 219 else if ( playersTotal == 21 ) 220 GameOver( GameStatus.BLACKJACK ); // if dealer has 21, dealer wins 223 else if ( dealersTotal == 21 ) 224 GameOver( GameStatus.LOSE ); dealerCard = 2; 227 playerCard = 13; } // end method dealButton_Click 230 Deal two cards to dealer but only display the face of the first one If both hands equal 21, it is a push If only the player has 21, the player wins If only the dealer has 21, the dealer wins

39 2002 Prentice Hall. All rights reserved. Outline 39 Blackjack.cs 231 // displays card represented by cardValue in 232 // PictureBox with number card 233 public void DisplayCard( int card, string cardValue ) 234 { 235 // retrieve appropriate PictureBox from ArrayList 236 PictureBox displayBox = ( PictureBox ) cardBoxes[ card ]; // if string representing card is empty, 239 // set displayBox to display back of card 240 if ( cardValue == "" ) 241 { 242 displayBox.Image = 243 Image.FromFile( "blackjack_images\\cardback.png" ); 244 return; 245 } // retrieve face value of card from cardValue 248 int faceNumber = Int32.Parse( cardValue.Substring( 0, 249 cardValue.IndexOf( " " ) ) ); string face = faceNumber.ToString(); // retrieve the suit of the card from cardValue 254 string suit = cardValue.Substring( 255 cardValue.IndexOf( " " ) + 1 ); char suitLetter; // determine if suit is other than clubs 260 switch ( Convert.ToInt32( suit ) ) 261 { 262 // suit is clubs 263 case 0: 264 suitLetter = 'c'; 265 break; If cardValue is an empty string, display the back of the card image Retrieve face and suit of card from cardValue

40 2002 Prentice Hall. All rights reserved. Outline 40 Blackjack.cs // suit is diamonds 268 case 1: 269 suitLetter = 'd'; 270 break; // suit is hearts 273 case 2: 274 suitLetter = 'h'; 275 break; // else suit is spades 278 default: 279 suitLetter = 's'; 280 break; 281 } // set displayBox to display appropriate image 284 displayBox.Image = Image.FromFile( 285 "blackjack_images\\" + face + suitLetter + ".png" ); } // end method DisplayCard // displays all player cards and shows 290 // appropriate game status message 291 public void GameOver( GameStatus winner ) 292 { 293 char[] tab = { '\t' }; 294 string[] cards = dealersCards.Split( tab ); for ( int i = 0; i < cards.Length; i++ ) 297 DisplayCard( i, cards[ i ] ); 298 Display all of dealers cards

41 2002 Prentice Hall. All rights reserved. Outline 41 Blackjack.cs 299 // push 300 if ( winner == GameStatus.PUSH ) 301 statusLabel.Text = "It's a tie!"; // player loses 304 else if ( winner == GameStatus.LOSE ) 305 statusLabel.Text = "You Lose Try Again!"; // player wins 308 else if ( winner == GameStatus.WIN ) 309 statusLabel.Text = "You Win!"; // player has won with blackjack 312 else 313 statusLabel.Text = "BlackJack!"; stayButton.Enabled = false; 316 hitButton.Enabled = false; 317 dealButton.Enabled = true; } // end method GameOver } // end class Blackjack Display appropriate message in statusLabel

42 2002 Prentice Hall. All rights reserved. Outline 42 Blackjack.cs Program Output

43 2002 Prentice Hall. All rights reserved. Outline 43 Blackjack.cs Program Output

44 2002 Prentice Hall. All rights reserved. Outline 44 Blackjack.cs Program Output

45 2002 Prentice Hall. All rights reserved. Outline 45 Reservation.asmx.cs 1 // Fig : Reservation.asmx.cs 2 // Airline reservation Web Service. 3 4 using System; 5 using System.Data; 6 using System.Diagnostics; 7 using System.Web; 8 using System.Web.Services; 9 using System.Data.OleDb; namespace AirlineReservation 12 { 13 // performs reservation of a seat 14 [ WebService( Namespace = "http://www.deitel.com/csphtp1/ch21/", 15 Description = "Service that enables a user to " + 16 "reserve a seat on a plane." ) ] 17 public class Reservation : System.Web.Services.WebService 18 { 19 private System.Data.OleDb.OleDbCommand 20 oleDbSelectCommand1; 21 private System.Data.OleDb.OleDbCommand 22 oleDbInsertCommand1; 23 private System.Data.OleDb.OleDbCommand 24 oleDbUpdateCommand1; 25 private System.Data.OleDb.OleDbCommand 26 oleDbDeleteCommand1; 27 private System.Data.OleDb.OleDbConnection 28 oleDbConnection1; 29 private System.Data.OleDb.OleDbDataAdapter 30 oleDbDataAdapter1; // Visual Studio.NET generated code 33

46 2002 Prentice Hall. All rights reserved. Outline 46 Reservation.asmx.cs 34 // checks database to determine whether 35 // matching seat is available 36 [ WebMethod ( Description = "Method to reserve seat." ) ] 37 public bool Reserve( string seatType, string classType ) 38 { 39 OleDbDataReader dataReader; // try database connection 42 try 43 { 44 // open database connection 45 oleDbConnection1.Open(); // set and execute SQL query 48 oleDbDataAdapter1.SelectCommand.CommandText = 49 "SELECT Number FROM Seats WHERE Type = '" + 50 seatType + "' AND Class = '" + classType + 51 "' AND Taken = '0'" ; 52 dataReader = 53 oleDbDataAdapter1.SelectCommand.ExecuteReader(); // if there were results, seat is available 56 if ( dataReader.Read() ) 57 { 58 string seatNumber = dataReader.GetString( 0 ); dataReader.Close(); 61 Set a query to find seats of the specified criteria. Execute query. If Read method returned true, then query returned result(s) Retrieve the first string in the row (the seat number)

47 2002 Prentice Hall. All rights reserved. Outline 47 Reservation.asmx.cs 62 // update first available seat to be taken 63 oleDbDataAdapter1.UpdateCommand.CommandText = 64 "Update Seats Set Taken = '1' WHERE Number = '" 65 + seatNumber + "'"; 66 oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); return true; } // end if 71 dataReader.Close(); 72 } 73 catch ( OleDbException ) // if connection problem 74 { 75 return false; 76 } 77 finally 78 { 79 oleDbConnection1.Close(); 80 } // no seat was reserved 83 return false; } // end method Reserve } // end class Reservation } // end namespace AirlineReservation Set a query that updates the seat numbered seatNumber to be taken. Execute query.

48 2002 Prentice Hall. All rights reserved Using Web Forms and Web Services Fig Airline Web Service in design view.

49 2002 Prentice Hall. All rights reserved. Outline 49 TicketReservatio n.aspx Page language="c#" Codebehind="TicketReservation.aspx.cs" 6 AutoEventWireup="false" 7 Inherits="MakeReservation.TicketReservation" %> 16 Aisle 28 Middle 29 Window

50 2002 Prentice Hall. All rights reserved. Outline 50 TicketReservatio n.aspx 33 Economy 38 First Please select the type of seat and 50 class you wish to reserve:

51 2002 Prentice Hall. All rights reserved. Outline 51 TicketReservatio n.aspx.cs 1 // Fig : TicketReservation.aspx.cs 2 // Making a Reservation using a Web Service. 3 4 using System; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Data; 8 using System.Drawing; 9 using System.Web; 10 using System.Web.SessionState; 11 using System.Web.UI; 12 using System.Web.UI.WebControls; 13 using System.Web.UI.HtmlControls; namespace MakeReservation 16 { 17 // allows visitors to select seat type to reserve, and 18 // then make reservation 19 public class TicketReservation : System.Web.UI.Page 20 { 21 protected System.Web.UI.WebControls.DropDownList 22 seatList; 23 protected System.Web.UI.WebControls.DropDownList 24 classList; protected System.Web.UI.WebControls.Button 27 reserveButton; 28 protected System.Web.UI.WebControls.Label Label1; private localhost.Reservation agent = 31 new localhost.Reservation(); 32

52 2002 Prentice Hall. All rights reserved. Outline 52 TicketReservatio n.aspx.cs 33 private void Page_Load( 34 object sender, System.EventArgs e ) 35 { 36 if ( IsPostBack ) 37 { 38 seatList.Visible = false; 39 classList.Visible = false; 40 reserveButton.Visible = false; 41 Label1.Visible = false; 42 } 43 } // Visual Studio.NET generated code // calls Web Service to try to reserve specified seat 48 public void reserveButton_Click ( 49 object sender, System.EventArgs e ) 50 { 51 // if Web-service method returned true, signal success 52 if ( agent.Reserve( seatList.SelectedItem.Text, 53 classList.SelectedItem.Text ) ) 54 Response.Write( "Your reservation has been made." 55 + " Thank you." ); // Web-service method returned false, so signal failure 58 else 59 Response.Write( "This seat is not available, " + 60 "please hit the back button on your browser " + 61 "and try again." ); } // end method reserveButton_Click } // end class TicketReservation } // end namespace MakeReservation

53 2002 Prentice Hall. All rights reserved. Outline 53 TicketReservatio n.aspx.cs Program Output

54 2002 Prentice Hall. All rights reserved. Outline 54 TicketReservatio n.aspx.cs Program Output

55 2002 Prentice Hall. All rights reserved. Outline 55 TemperatureServe r.asmx.cs 1 // Fig : TemperatureServer.asmx.cs 2 // TemperatureServer Web Service that extracts weather 3 // information from a Web page. 4 5 using System; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Data; 9 using System.Diagnostics; 10 using System.Web; 11 using System.Web.Services; 12 using System.IO; 13 using System.Net; namespace TemperatureWebService 16 { 17 [ WebService( Namespace = "http://www.deitel.com/csphtp1/ch21/", 18 Description = "A Web service that provides information " + 19 "from the National Weather Service." ) ] 20 public class TemperatureServer : 21 System.Web.Services.WebService 22 { 23 // Visual Studio.NET generated code [ WebMethod( EnableSession = true, Description = 26 "Method to read information from the weather service." ) ] 27 public void UpdateWeatherConditions() 28 { 29 // create WebClient to get access to Web page 30 WebClient myClient = new WebClient(); 31 ArrayList cityList = new ArrayList(); 32

56 2002 Prentice Hall. All rights reserved. Outline 56 TemperatureServe r.asmx.cs 33 // get StreamReader for response so we can read page 34 StreamReader input = new StreamReader( 35 myClient.OpenRead( 36 "http://iwin.nws.noaa.gov/iwin/us/" + 37 "traveler.html" ) ); string separator = "TAV12"; // locate first horizontal line on Web page 42 while ( !input.ReadLine().StartsWith( 43 separator ) ) ; // do nothing // day format and night format 46 string dayFormat = 47 "CITY WEA HI/LO WEA " + 48 "HI/LO"; 49 string nightFormat = 50 "CITY WEA LO/HI WEA " + 51 "LO/HI"; 52 string inputLine = ""; // locate header that begins weather information 55 do 56 { 57 inputLine = input.ReadLine(); 58 } while ( !inputLine.Equals( dayFormat ) && 59 !inputLine.Equals( nightFormat ) ); // get first city's data 62 inputLine = input.ReadLine(); 63

57 2002 Prentice Hall. All rights reserved. Outline 57 TemperatureServe r.asmx.cs 64 while ( inputLine.Length > 28 ) 65 { 66 // create CityWeather object for city 67 CityWeather weather = new CityWeather( 68 inputLine.Substring( 0, 16 ), 69 inputLine.Substring( 16, 7 ), 70 inputLine.Substring( 23, 7 ) ); // add to List 73 cityList.Add( weather ); // get next city's data 76 inputLine = input.ReadLine(); 77 } // close connection to NWS server 80 input.Close(); // add city list to user session 83 Session.Add( "cityList", cityList ); } // end UpdateWeatherConditions // gets all city names 88 [ WebMethod( EnableSession = true, Description = 89 "Method to retrieve a list of cities." ) ] 90 public string[] Cities() 91 { 92 ArrayList cityList = ( ArrayList ) Session[ "cityList" ]; 93 string[] cities= new string[ cityList.Count ]; 94

58 2002 Prentice Hall. All rights reserved. Outline 58 TemperatureServe r.asmx.cs 95 // retrieve names for cities 96 for ( int i = 0; i < cityList.Count; i++ ) 97 { 98 CityWeather weather = ( CityWeather ) cityList[ i ]; cities[ i ] = weather.CityName; 101 } return cities; } // end method Cities // gets all city descriptions 108 [ WebMethod( EnableSession = true, Description = "Method" " to retrieve weather descriptions for a " "list of cities." )] 111 public string[] Descriptions() 112 { 113 ArrayList cityList = ( ArrayList ) Session[ "cityList" ]; 114 string[] descriptions= new string[ cityList.Count ]; // retrieve weather descriptions for all cities 117 for ( int i = 0; i < cityList.Count; i++ ) 118 { 119 CityWeather weather = ( CityWeather )cityList[ i ]; descriptions[ i ] = weather.Description; 122 } return descriptions; } // end method Descriptions 127

59 2002 Prentice Hall. All rights reserved. Outline 59 TemperatureServe r.asmx.cs 128 // obtains each city temperature 129 [ WebMethod( EnableSession = true, Description = "Method " "to retrieve the temperature for a list of cities." ) ] 131 public string[] Temperatures() 132 { 133 ArrayList cityList = ( ArrayList ) Session[ "cityList" ]; 134 string[] temperatures= new string[ cityList.Count ]; // retrieve temperatures for all cities 137 for ( int i = 0; i < cityList.Count; i++ ) 138 { 139 CityWeather weather = ( CityWeather )cityList[ i ]; 140 temperatures[ i ] = weather.Temperature; 141 } return temperatures; } // end method Temperatures } // end class TemperatureServer } // end namespace TemperatureWebService

60 2002 Prentice Hall. All rights reserved. Outline 60 CityWeather.cs 1 // Fig : CityWeather.cs 2 // Class representing the weather information for one city. 3 4 using System; 5 6 namespace TemperatureWebService 7 { 8 public class CityWeather 9 { 10 private string cityName; 11 private string temperature; 12 private string description; public CityWeather( 15 string city, string information, string degrees ) 16 { 17 cityName = city; 18 description = information; 19 temperature = degrees; 20 } // city name 23 public string CityName 24 { 25 get 26 { 27 return cityName; 28 } 29 } 30

61 2002 Prentice Hall. All rights reserved. Outline 61 CityWeather.cs 31 // city temperature 32 public string Temperature 33 { 34 get 35 { 36 return temperature; 37 } 38 } // forecast description 41 public string Description 42 { 43 get 44 { 45 return description; 46 } 47 } } // end class CityWeather 50 } // end namespace TemperatureWebService

62 2002 Prentice Hall. All rights reserved. Outline 62 Client.cs 1 // Fig : Client.cs 2 // Class that displays weather information that it receives 3 // from a Web service. 4 5 using System; 6 using System.Drawing; 7 using System.Collections; 8 using System.ComponentModel; 9 using System.Windows.Forms; 10 using System.Net; namespace TemperatureClient 13 { 14 public class Client : System.Windows.Forms.Form 15 { 16 private System.Windows.Forms.Label label1; 17 private System.Windows.Forms.Label label2; 18 private System.Windows.Forms.Label label3; 19 private System.Windows.Forms.Label label4; 20 private System.Windows.Forms.Label label5; 21 private System.Windows.Forms.Label label6; 22 private System.Windows.Forms.Label label7; 23 private System.Windows.Forms.Label label8; 24 private System.Windows.Forms.Label label9; 25 private System.Windows.Forms.Label label10; 26 private System.Windows.Forms.Label label11; 27 private System.Windows.Forms.Label label12; 28 private System.Windows.Forms.Label label13; 29 private System.Windows.Forms.Label label14; 30 private System.Windows.Forms.Label label15; 31 private System.Windows.Forms.Label label16; 32 private System.Windows.Forms.Label label17; 33 private System.Windows.Forms.Label label18; 34 private System.Windows.Forms.Label label19; 35 private System.Windows.Forms.Label label20;

63 2002 Prentice Hall. All rights reserved. Outline 63 Client.cs 36 private System.Windows.Forms.Label label21; 37 private System.Windows.Forms.Label label22; 38 private System.Windows.Forms.Label label23; 39 private System.Windows.Forms.Label label24; 40 private System.Windows.Forms.Label label25; 41 private System.Windows.Forms.Label label26; 42 private System.Windows.Forms.Label label27; 43 private System.Windows.Forms.Label label28; 44 private System.Windows.Forms.Label label29; 45 private System.Windows.Forms.Label label30; 46 private System.Windows.Forms.Label label31; 47 private System.Windows.Forms.Label label32; 48 private System.Windows.Forms.Label label33; 49 private System.Windows.Forms.Label label34; 50 private System.Windows.Forms.Label label36; 51 private System.Windows.Forms.Label label35; private System.ComponentModel.Container components = 54 null; public Client() 57 { 58 InitializeComponent(); localhost.TemperatureServer client = 61 new localhost.TemperatureServer(); 62 client.CookieContainer = new CookieContainer(); 63 client.UpdateWeatherConditions(); string[] cities = client.Cities(); 66 string[] descriptions = client.Descriptions(); 67 string[] temperatures = client.Temperatures(); label35.BackgroundImage = new Bitmap( 70 "images/header.png" );

64 2002 Prentice Hall. All rights reserved. Outline 64 Client.cs 71 label36.BackgroundImage = new Bitmap( 72 "images/header.png" ); // create Hashtable and populate it with every label 75 Hashtable cityLabels = new Hashtable(); 76 cityLabels.Add( 1, label1 ); 77 cityLabels.Add( 2, label2 ); 78 cityLabels.Add( 3, label3 ); 79 cityLabels.Add( 4, label4 ); 80 cityLabels.Add( 5, label5 ); 81 cityLabels.Add( 6, label6 ); 82 cityLabels.Add( 7, label7 ); 83 cityLabels.Add( 8, label8 ); 84 cityLabels.Add( 9, label9 ); 85 cityLabels.Add( 10, label10 ); 86 cityLabels.Add( 11, label11 ); 87 cityLabels.Add( 12, label12 ); 88 cityLabels.Add( 13, label13 ); 89 cityLabels.Add( 14, label14 ); 90 cityLabels.Add( 15, label15 ); 91 cityLabels.Add( 16, label16 ); 92 cityLabels.Add( 17, label17 ); 93 cityLabels.Add( 18, label18 ); 94 cityLabels.Add( 19, label19 ); 95 cityLabels.Add( 20, label20 ); 96 cityLabels.Add( 21, label21 ); 97 cityLabels.Add( 22, label22 ); 98 cityLabels.Add( 23, label23 ); 99 cityLabels.Add( 24, label24 ); 100 cityLabels.Add( 25, label25 ); 101 cityLabels.Add( 26, label26 ); 102 cityLabels.Add( 27, label27 ); 103 cityLabels.Add( 28, label28 ); 104 cityLabels.Add( 29, label29 ); 105 cityLabels.Add( 30, label30 );

65 2002 Prentice Hall. All rights reserved. Outline 65 Client.cs 106 cityLabels.Add( 31, label31 ); 107 cityLabels.Add( 32, label32 ); 108 cityLabels.Add( 33, label33 ); 109 cityLabels.Add( 34, label34 ); // create Hashtable and populate with 112 // all weather conditions 113 Hashtable weather = new Hashtable(); 114 weather.Add( "SUNNY", "sunny" ); 115 weather.Add( "PTCLDY", "pcloudy" ); 116 weather.Add( "CLOUDY", "mcloudy" ); 117 weather.Add( "MOCLDY", "mcloudy" ); 118 weather.Add( "TSTRMS", "rain" ); 119 weather.Add( "RAIN", "rain" ); 120 weather.Add( "SNOW", "snow" ); 121 weather.Add( "VRYHOT", "vryhot" ); 122 weather.Add( "FAIR", "fair" ); 123 weather.Add( "RNSNOW", "rnsnow" ); 124 weather.Add( "SHWRS", "showers" ); 125 weather.Add( "WINDY", "windy" ); 126 weather.Add( "NOINFO", "noinfo" ); 127 weather.Add( "MISG", "noinfo" ); 128 weather.Add( "DRZL", "rain" ); 129 weather.Add( "HAZE", "noinfo" ); 130 weather.Add( "SMOKE", "mcloudy" ); Bitmap background = new Bitmap( "images/back.png" ); 133 Font font = new Font( "Courier New", 8, 134 FontStyle.Bold ); // for every city 137 for ( int i = 0; i < cities.Length; i++ ) 138 { 139 // use Hashtable cityLabels to find the next Label 140 Label currentCity = ( Label )cityLabels[ i + 1 ];

66 2002 Prentice Hall. All rights reserved. Outline 66 Client.cs // set current Label's image to image 143 // corresponding to the city's weather condition // find correct image name in Hashtable weather 145 currentCity.Image = new Bitmap( "images/" weather[ descriptions[ i ].Trim() ] + ".png" ); // set background image, font and forecolor 149 // of Label 150 currentCity.BackgroundImage = background; 151 currentCity.Font = font; 152 currentCity.ForeColor = Color.White; // set label's text to city name 155 currentCity.Text = "\r\n" + cities[ i ] + " " temperatures[ i ]; 157 } } // end of constructor // Visual Studio.NET generated code [STAThread] 164 static void Main() 165 { 166 Application.Run( new Client() ); 167 } } // end class Client } // end namespace TemperatureClient

67 2002 Prentice Hall. All rights reserved. Outline 67 Client.cs Program Output

68 2002 Prentice Hall. All rights reserved. Outline 68 Equation.cs 1 // Fig : Equation.cs 2 // Class Equation that contains 3 // information about an equation. 4 5 using System; 6 7 public class Equation 8 { 9 private int left, right, result; 10 private string operation; // required default constructor 13 public Equation() : this( 0, 0, "+" ) 14 { 15 } // constructor for class Equation 18 public Equation( int leftValue, int rightValue, 19 string operationType ) 20 { 21 Left = leftValue; 22 Right = rightValue; 23 Operation = operationType; switch ( operationType ) 26 { 27 case "+": 28 Result = Left + Right; 29 break; 30 case "-": 31 Result = Left - Right; 32 break;

69 2002 Prentice Hall. All rights reserved. Outline 69 Equation.cs 33 case "*": 34 Result = Left * Right; 35 break; 36 } 37 } public override string ToString() 40 { 41 return Left.ToString() + " " + Operation + " " + 42 Right.ToString() + " = " + Result.ToString(); 43 } // property returning string representing 46 // left-hand side 47 public string LeftHandSide 48 { 49 get 50 { 51 return Left.ToString() + " " + Operation + " " + 52 Right.ToString(); 53 } set 56 { 57 } 58 } // property returning string representing 61 // right-hand side 62 public string RightHandSide 63 { 64 get 65 { 66 return Result.ToString(); 67 }

70 2002 Prentice Hall. All rights reserved. Outline 70 Equation.cs set 70 { 71 } 72 } // left operand get and set property 75 public int Left 76 { 77 get 78 { 79 return left; 80 } set 83 { 84 left = value; 85 } 86 } // right operand get and set property 89 public int Right 90 { 91 get 92 { 93 return right; 94 } set 97 { 98 right = value; 99 } 100 } 101

71 2002 Prentice Hall. All rights reserved. Outline 71 Equation.cs 102 // get and set property of result of applying 103 // operation to left and right operands 104 public int Result 105 { 106 get 107 { 108 return result; 109 } set 112 { 113 result = value; 114 } 115 } // get and set property for operation 118 public string Operation 119 { 120 get 121 { 122 return operation; 123 } set 126 { 127 operation = value; 128 } 129 } } // end class Equation

72 2002 Prentice Hall. All rights reserved. Outline 72 Generator.asmx.c s 1 // Fig : Generator.asmx.cs 2 // Web Service to generate random equations based on a 3 // specified operation and difficulty level. 4 5 using System; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Data; 9 using System.Diagnostics; 10 using System.Web; 11 using System.Web.Services; namespace EquationGenerator 14 { 15 [ WebService( Namespace = "http://www.deitel.com/csphtp1/ch21", 16 Description = "A Web service that generates questions " + 17 "based on the specified mathematical operation and " + 18 "level of difficulty chosen." ) ] 19 public class Generator : System.Web.Services.WebService 20 { // Visual Studio.NET generated code [ WebMethod ( Description = 25 "Method that generates a random equation." ) ] 26 public Equation GenerateEquation( string operation, 27 int level ) 28 { 29 // find maximum and minimum number to be used 30 int maximum = ( int ) Math.Pow( 10, level ), 31 minimum = ( int ) Math.Pow( 10, level - 1 ); 32

73 2002 Prentice Hall. All rights reserved. Outline 73 Generator.asmx.c s 33 Random random = new Random(); // create equation consisting of two random numbers 36 // between minimum and maximum parameters 37 Equation equation = new Equation( 38 random.Next( minimum, maximum ), 39 random.Next( minimum, maximum ), operation ); return equation; } // end method GenerateEquation } // end class Generator } // end namespace EquationGenerator

74 2002 Prentice Hall. All rights reserved User Defined Types in Web Services Fig Returning an object from a Web-service method. (Part 1) Create a subtraction exercise Make exercise of user skill level 2

75 2002 Prentice Hall. All rights reserved User Defined Types in Web Services Fig Returning an object from a Web-service method. (Part 2)

76 2002 Prentice Hall. All rights reserved. Outline 76 Tutor.cs 1 // Fig : Tutor.cs 2 // Math tutor program. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 10 namespace EquationGeneratorClient 11 { 12 public class Tutor : System.Windows.Forms.Form 13 { 14 private System.Windows.Forms.Panel panel1; 15 private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Label questionLabel; 18 private System.Windows.Forms.TextBox answerTextBox; 19 private System.Windows.Forms.Button okButton; 20 private System.Windows.Forms.Button generateButton; private System.Windows.Forms.RadioButton oneRadioButton; 23 private System.Windows.Forms.RadioButton twoRadioButton; 24 private System.Windows.Forms.RadioButton 25 threeRadioButton; 26 private System.Windows.Forms.RadioButton addRadioButton; 27 private System.Windows.Forms.RadioButton 28 subtractRadioButton; 29 private System.Windows.Forms.RadioButton 30 multiplyRadioButton; private System.ComponentModel.Container components = 33 null; 34 private int level = 1; 35

77 2002 Prentice Hall. All rights reserved. Outline 77 Tutor.cs 36 private localhost.Equation equation; 37 private localhost.Generator generator = 38 new localhost.Generator(); 39 private string operation = "+"; // Visual Studio.NET generated code [STAThread] 44 static void Main() 45 { 46 Application.Run( new Tutor() ); 47 } // generates new equation on click event 50 protected void generateButton_Click( object sender, 51 System.EventArgs e ) 52 { 53 // generate equation using current operation 54 // and level 55 equation = generator.GenerateEquation( operation, 56 level ); // display left-hand side of equation 59 questionLabel.Text = equation.LeftHandSide; okButton.Enabled = true; 62 answerTextBox.Enabled = true; } // end method generateButton_Click 65

78 2002 Prentice Hall. All rights reserved. Outline 78 Tutor.cs 66 // check users answer 67 protected void okButton_Click( object sender, 68 System.EventArgs e ) 69 { 70 // determine correct result from Equation 71 // object 72 int answer = equation.Result; // get user's answer 75 int myAnswer = Int32.Parse( answerTextBox.Text ); // test if user's answer is correct 78 if ( answer == myAnswer ) 79 { 80 questionLabel.Text = ""; 81 answerTextBox.Text = ""; 82 okButton.Enabled = false; 83 MessageBox.Show( "Correct! Good job!" ); 84 } 85 else 86 MessageBox.Show( "Incorrect. Try again." ); } // end method okButton_Click // set the selected operation 91 protected void operationRadioButtons_Click( object sender, 92 EventArgs e ) 93 { 94 RadioButton item = ( RadioButton ) sender; 95

79 2002 Prentice Hall. All rights reserved. Outline 79 Tutor.cs 96 // set the operation to be the appropriate symbol 97 if ( item == addRadioButton ) 98 operation = "+"; 99 else if ( item == subtractRadioButton ) 100 operation = "-"; 101 else 102 operation = "*"; generateButton.Text = "Generate " + item.Text " Example"; } // end method operationRadioButtons_Click // set the current level 110 protected void levelRadioButtons_Click( object sender, 111 EventArgs e ) 112 { 113 if ( sender == oneRadioButton ) 114 level = 1; 115 else if ( sender == twoRadioButton ) 116 level = 2; 117 else 118 level = 3; } // end method levelRadioButtons_Click } // end class Tutor } // end namespace EquationGeneratorClient

80 2002 Prentice Hall. All rights reserved. Outline 80 Tutor.cs Program Output

81 2002 Prentice Hall. All rights reserved. Outline 81 Tutor.cs Program Output


Download ppt "2002 Prentice Hall. All rights reserved. 1 Chapter 21 – ASP.NET and Web Services Outline 21.1 Introduction 21.2 Web Services 21.3 Simple Object Access."

Similar presentations


Ads by Google