1. Trang chủ
  2. » Công Nghệ Thông Tin

Teach Yourself the C# Language in 21 Days phần 9 ppsx

81 347 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 81
Dung lượng 1,07 MB

Nội dung

184: new System.Drawing.Font(“Microsoft Sans Serif”, 185: 20.25F, 186: System.Drawing.FontStyle.Regular, 187: System.Drawing.GraphicsUnit.Point, 188: ((System.Byte)(0))); 189: this.btnBottomMiddle.Location = new System.Drawing.Point(96, 160); 190: this.btnBottomMiddle.Name = “btnBottomMiddle”; 191: this.btnBottomMiddle.Size = new System.Drawing.Size(64, 56); 192: this.btnBottomMiddle.TabIndex = 7; 193: this.btnBottomMiddle.Click += 194: new System.EventHandler(this.btnTicTac_Click); 195: // 196: // btnBottomLeft 197: // 198: this.btnBottomLeft.Font = 199: new System.Drawing.Font(“Microsoft Sans Serif”, 200: 20.25F, 201: System.Drawing.FontStyle.Regular, 202: System.Drawing.GraphicsUnit.Point, 203: ((System.Byte)(0))); 204: this.btnBottomLeft.Location = new System.Drawing.Point(16, 160); 205: this.btnBottomLeft.Name = “btnBottomLeft”; 206: this.btnBottomLeft.Size = new System.Drawing.Size(64, 56); 207: this.btnBottomLeft.TabIndex = 6; 208: this.btnBottomLeft.Click += 209: new System.EventHandler(this.btnTicTac_Click) ; 210: // 211: // btnNewGame 212: // 213: this.btnNewGame.Location = new System.Drawing.Point(16, 248); 214: this.btnNewGame.Name = “btnNewGame”; 215: this.btnNewGame.Size = new System.Drawing.Size(80, 24); 216: this.btnNewGame.TabIndex = 9; 217: this.btnNewGame.Text = “New Game”; 218: this.btnNewGame.Click += 219: new System.EventHandler(this.btnNewGame_Click); 220: // 221: // btnExit 222: // 223: this.btnExit.Location = new System.Drawing.Point(160, 248); 224: this.btnExit.Name = “btnExit”; 225: this.btnExit.Size = new System.Drawing.Size(80, 24); 226: this.btnExit.TabIndex = 10; 227: this.btnExit.Text = “Exit”; 228: this.btnExit.Click += new System.EventHandler(this.btnExit_Click); 229: // 230: // btnTurn 231: // 232: this.btnTurn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 622 Type & Run 4 LISTING T&R 4.1 continued Tic Tac Toe 623 233: this.btnTurn.Font = 234: new System.Drawing.Font(“Microsoft Sans Serif”, 235: 20.25F, 236: System.Drawing.FontStyle.Regular, 237: System.Drawing.GraphicsUnit.Point, 238: ((System.Byte)(0))); 239: this.btnTurn.Location = new System.Drawing.Point(112, 232); 240: this.btnTurn.Name = “btnTurn”; 241: this.btnTurn.Size = new System.Drawing.Size(32, 40); 242: this.btnTurn.TabIndex = 0; 243: this.btnTurn.TabStop = false; 244: this.btnTurn.Text = “X”; 245: // 246: // panel1 247: // 248: this.panel1.BackColor = System.Drawing.Color.Black; 249: this.panel1.Location = new System.Drawing.Point(16, 16); 250: this.panel1.Name = “panel1”; 251: this.panel1.Size = new System.Drawing.Size(224, 200); 252: // 253: // TicTac 254: // 255: this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 256: this.ClientSize = new System.Drawing.Size(256, 286); 257: this.Controls.Add(this.btnTurn); 258: this.Controls.Add(this.btnExit); 259: this.Controls.Add(this.btnNewGame); 260: this.Controls.Add(this.btnBottomRight); 261: this.Controls.Add(this.btnBottomMiddle); 262: this.Controls.Add(this.btnBottomLeft); 263: this.Controls.Add(this.btnMiddleRight); 264: this.Controls.Add(this.btnMiddleMiddle); 265: this.Controls.Add(this.btnMiddleLeft); 266: this.Controls.Add(this.btnTopRight); 267: this.Controls.Add(this.btnTopMiddle); 268: this.Controls.Add(this.btnTopLeft); 269: this.Controls.Add(this.panel1); 270: this.FormBorderStyle = FormBorderStyle.Fixed3D; 271: this.MaximizeBox = false; 272: this.Name = “TicTac”; 273: this.Text = “Tic Tac Toe”; 274: this.ResumeLayout(false); 275: 276: } 277: 278: /// <summary> 279: /// The main entry point for the application. 280: /// </summary> 281: public static void Main() LISTING T&R 4.1 continued 282: { 283: Application.Run(new TicTac()); 284: } 285: 286: private string setText(string origText) 287: { 288: string tmpText = origText; 289: if (origText == “X” || origText == “O” ) 290: { 291: // Already a character in section 292: 293: } 294: else 295: { 296: tmpText = btnTurn.Text; 297: if ( btnTurn.Text == “X”) 298: { 299: btnTurn.Text = “O”; 300: } 301: else 302: { 303: btnTurn.Text = “X”; 304: } 305: turn++; // turn successful, so count. 306: } 307: return tmpText; 308: } 309: 310: // Check to see if game is over 311: // val == character for turn. 312: private void checkEndGame(string val) 313: { 314: bool gameover = false; 315: 316: // First check for a winner 317: if( btnTopLeft.Text == val ) 318: { 319: if( btnTopMiddle.Text == val && 320: btnTopRight.Text == val ) 321: { 322: gameover = true; 323: } 324: if( btnMiddleLeft.Text == val && 325: btnBottomLeft.Text == val ) 326: { 327: gameover = true; 328: } 329: if( btnMiddleMiddle.Text == val && 330: btnBottomRight.Text == val) 624 Type & Run 4 LISTING T&R 4.1 continued Tic Tac Toe 625 331: { 332: gameover = true; 333: } 334: } 335: 336: if(btnTopMiddle.Text == val) 337: { 338: if(btnMiddleMiddle.Text == val && 339: btnBottomMiddle.Text == val) 340: { 341: gameover = true; 342: } 343: } 344: 345: if(btnMiddleLeft.Text == val) 346: { 347: if(btnMiddleMiddle.Text == val && 348: btnMiddleRight.Text == val) 349: { 350: gameover = true; 351: } 352: } 353: 354: if( btnBottomLeft.Text == val ) 355: { 356: if( btnBottomMiddle.Text == val && 357: btnBottomRight.Text == val) 358: { 359: gameover = true; 360: } 361: if( btnMiddleMiddle.Text == val && 362: btnTopRight.Text == val ) 363: { 364: gameover = true; 365: } 366: } 367: 368: if( btnTopRight.Text == val) 369: { 370: if( btnMiddleRight.Text == val && 371: btnBottomRight.Text == val) 372: { 373: gameover = true; 374: } 375: } 376: 377: // Check to see if game over because of win 378: if( gameover == true) 379: { LISTING T&R 4.1 continued 380: if ( val == “X” ) 381: MessageBox.Show( “Game Over - X wins!”, 382: “Game Over!” ); 383: else 384: MessageBox.Show( “Game Over - O wins!”, 385: “Game Over!”); 386: } 387: else 388: { 389: // no winner, are all nine spaces filled? 390: if( turn >= 9 ) 391: { 392: // game over do end game stuff 393: MessageBox.Show(“Game Over - No winner!”); 394: gameover = true; 395: } 396: } 397: 398: // See if board needs reset. 399: if (gameover == true) 400: { 401: resetGame(); 402: } 403: } 404: 405: private void btnExit_Click(object sender, System.EventArgs e) 406: { 407: Application.Exit(); 408: } 409: 410: private void btnNewGame_Click(object sender, System.EventArgs e) 411: { 412: resetGame(); 413: } 414: 415: private void resetGame() 416: { 417: turn = 0; 418: btnTopLeft.Text = “ “; 419: btnTopMiddle.Text = “ “; 420: btnTopRight.Text = “ “; 421: btnMiddleLeft.Text = “ “; 422: btnMiddleMiddle.Text = “ “; 423: btnMiddleRight.Text = “ “; 424: btnBottomLeft.Text = “ “; 425: btnBottomMiddle.Text = “ “; 426: btnBottomRight.Text = “ “; 427: btnTurn.Text = “X”; 428: } 626 Type & Run 4 LISTING T&R 4.1 continued Tic Tac Toe 627 429: 430: // Set X or O text on grid button 431: private void btnTicTac_Click(object sender, System.EventArgs e) 432: { 433: // convert the sender object to a button: 434: Button tmpButton = (Button) sender; 435: // Set the text of this button: 436: tmpButton.Text = setText(tmpButton.Text); 437: 438: checkEndGame(tmpButton.Text); 439: } 440: } 441: } 442: // End of Listing This is a Windows application, so you will want to target the compiling as a winexe. With the Microsoft C# command-line compiler, this is done by using the /target: or /t: flags. Additionally, you may need to reference the windows forms classes in the com- mand line: csc /r:System.Windows.Forms.dll /t:winexe TicTac.cs LISTING T&R 4.1 continued Depending on your compiler, you may not need to include the reference to System.Windows.Forms.dll in the command line. Note After it is compiled, running the program presents the form presented in Figure TR4.1. F IGURE TR4.1 The Tic Tac Toe appli- cation. 628 Type & Run 4 The source code for this listing is available on the included CD. Any updates to the code will be available at www.TeachYourselfCSharp.com. Note DAY 18 WEEK 3 Working with Databases: ADO.NET You’ve learned about working with classes within the .NET Framework. You’ve even learned about working with the windows forms classes. Today you con- tinue your learning with a different set of classes within the .NET Framework. These classes are focused on accessing and manipulating data. Today you… • Review key database terminology. • Learn about ADO.NET. • Use ADO.NET to retrieve data from a database. • Discover how to use a DataReader. • Add data to a database. Understanding Key Database Concepts On Day 15, “Using Existing Routines from the .NET Base Classes,” you learned how to read and write simple text files using streams. Using classes such as StreamWriter, StreamReader, BinaryWriter, and FileStream, you were able to both read information from different files and write information. In real-world applications, you will often need a more robust set of classes and routines to work with data and information. Instead of storing everything as pure text or pure binary information, you will want to store items as different data types with different characteristics. You won’t want to store integers and strings; instead, you will want to store information such as prices and titles. Information such as this is best stored in a database. Data becomes more useful when it is stored in a grouping. For example, if you create an application to track videos that are available for a rental, you might have a group of information that describes the videos and a group of information that describes the cus- tomers. For customers, this information may include their name, address, phone number, and membership number, along with the date they obtained rental privileges. You may also keep information on the media in the store. For example, you may keep track of title, rating, length, format, release date, and price to buy. A third set of information that you might want to track is the videos that a customer rented and when he or she rented them. Obviously, a lot more information can be tracked; however, most of this informa- tion can be grouped. If you started to write programs using the file classes you learned on Day 15, you would find that it would take a lot of work to simply read in a date or a dollar number. Additionally, storing the information in a straight text or binary file would not be very efficient. Instead, information can be stored in databases such as Oracle, Microsoft SQL Server, mySQL, and Microsoft Access. A database system such as these helps organize, store, and access the information. Understanding the Terminology A single piece of information, such as a name or an address, is called a field. Another name for a field is a column; you’ll understand why in a second. A 630 Day 18 Be aware that entire books have been written on the topic of data pro- gramming with C# and .NET. In today’s lesson, you will learn quite a bit; however, this just scratches the surface of everything there is to learn about database programming with C#. Note NEW TERM Working with Databases: ADO.NET 631 18 group of related fields is called a record. Another name for a record is a column. An example of a record or column is the information traced on a video—this may be the title, media, price, and other information. A set of records, or information on a number of titles, is kept as a fil. A file is also known as a table. A group of one or more related files is considered a database. You’ve just learned a lot of terms. In general, the terms row, column, and table are used within the context of .NET. These terms come from the fact that you can present data in a table format, as shown in Figure 18.1. One other term worth mentioning here is dataset. A dataset is one or more tables that have been copied from a database into memory. Note Introducing ADO.NET Because most real-world applications use data stored in a database, it shouldn’t be a sur- prise that strong database support is a part of the .NET Framework. Regardless of whether your data is stored on your local machine, on a network, or somewhere across the Internet, routines exist to help you access that information. F IGURE 18.1 Tables, rows, and columns. Rows/Records Columns/Fields Table: Videos [...]... $14 .99 Blood Wake (Teen) - $ 49. 99 Boys Don’t Cry (R) - $9. 99 BraveHeart (R) - $24 .99 Cast Away (PG-13) - $ 19. 99 Charlie’s Angels (PG-13) - $14 .99 Cider House Rules, The (PG-13) - $9 99 Contender, The (R) - $9. 99 Contender, The (R) - $9. 99 Crouching Tiger Hidden dragon (PG-13) - $16 .99 Dinotopia (NR) - $14 .99 Double Jeopardy (R) - $12 .99 Enemy of the State (R) - $1.50 Erin Brockovich (R) - $9. 99 Excallibur... (R) - $9. 99 Family Man, The (PG-13) - $12 .99 For Love of the Game (PG-13) - $9. 99 Fuzion Frenzy (Everyone) - $ 49. 99 640 Day 18 DVD DVD XBx DVD VHS DVD - GalaxyQuest (PG) - $16 .99 Gladiator (R) - $ 19. 99 Halo Combat Evolved (Mature) - $ 49. 99 Harry Potter and the Sorcerer’s Stone (PG) - $12 .99 Hollow Man (R) - $9. 99 Independence Day Collector’s Edition (PG-13) - $ 19. 99 DVD - X-Men (PG-13) - $14 .99 VHS... that, using ADO.NET classes, information is copied from the database into the memory of your own computer You can then disconnect from the database After manipulating the data in memory, you can again connect to the database and have the changes made Connecting to and Working with a Database NEW TERM Connecting to the database, retrieving data from the database, updating or delet- ing the data in the database,... three strings are assigned This is not the case The plus sign adds the three strings (concatenates them) into a single string In Lines 20 21, two variables are set up to track totals These totals will be filled in as the records are read In Line 22 a SQL query is created and also assigned to a string variable, mySelectQuery This query selects all the information from each row in the videos table It then... it is simply printed to the console ANALYSIS Stepping back, you can see that much of the logic for connecting and opening the database is the same as in the previous listing Where things start to truly differ is in Line 42 Instead of calling the ExecuteReader method, this line calls the ExecuteNonQuery method This executes the myAddQuery that was associated to myCommand in Lines 37–38 The call to ExecuteNonQuery... yourself The Web server calls the correct compiler based on the language specified in the Language= command The next change that you can see is the inclusion of the System.Web.Service namespace in Line 8 This is included so that the use of WebMethod and WebService can be done without explicitly including the namespace name throughout the rest of the listing In Line 10, the Calc class is derived from... ExecuteNonQuery results in the query being executed and a number being returned that is equal to the number of rows affected In this case, a single line was added, so the value 1 should be returned 18 644 Day 18 After Line 42 completes, the exception-handling logic directs the program’s flow to the finally clause in Line 48 In Line 50, a check is made to see whether the connection to the database was actually... means of accessing and manipulating data in a database However, this is not the only means There are other ways, including a more robust way to work with data from databases This includes the use of DataSets and DataAdapters There is also the capability to do data binding to controls Finally, you can work with data in other formats, including XML This can be done in the same manner as working with databases... listing ends with the values of the counter variables being printed in Line 55 This prints the total value of the videos along with the average value I may sound like a broken record (or a CD with a skip), but this listing is missing exception handling In an exercise at the end of today, exception handling will be added You should always include exception handling in your applications Adding, Updating,... enter the parameters that your method expects FIGURE 19. 3 The Add method within the Web service In the case of the Add method, two parameters are expected: x and y This matches your code in Listing 19. 3 If you enter the values of 5 and 10, as you did in Listing 19. 2, you will see the result: 15 The result of 15 is in there, . (PG-13) - $ 19. 99 DVD - Charlie’s Angels (PG-13) - $14 .99 DVD - Cider House Rules, The (PG-13) - $9. 99 DVD - Contender, The (R) - $9. 99 VHS - Contender, The (R) - $9. 99 DVD - Crouching Tiger Hidden. $16 .99 DVD - Dinotopia (NR) - $14 .99 VHS - Double Jeopardy (R) - $12 .99 DVD - Enemy of the State (R) - $1.50 DVD - Erin Brockovich (R) - $9. 99 DVD - Excallibur (R) - $9. 99 DVD - Family Man, The. (PG-13) - $12 .99 DVD - For Love of the Game (PG-13) - $9. 99 XBx - Fuzion Frenzy (Everyone) - $ 49. 99 LISTING 18.1 continued OUTPUT DVD - GalaxyQuest (PG) - $16 .99 DVD - Gladiator (R) - $ 19. 99 XBx -

Ngày đăng: 13/08/2014, 08:20

TỪ KHÓA LIÊN QUAN