Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Microsoft C# Programming for the Absolute Beginner

Table of Contents

 

Microsoft C# Programming for the Absolute Beginner.................................................................

1

Introduction........................................................................................................................................

4

Overview..................................................................................................................................

4

Chapter 1: Basic Input and Output: A Mini Adventure...................................................................

5

Project: The Mini Adventure.....................................................................................................

5

Reviewing Basic C# Concepts.................................................................................................

6

Namespaces......................................................................................................................

7

Classes..............................................................................................................................

7

Methods.............................................................................................................................

7

Statements.........................................................................................................................

7

The Console Object...........................................................................................................

8

.NET Documentation..........................................................................................................

8

Saying “Hello, World!”............................................................................................................

12

Getting into the Visual Studio .Net Environment..............................................................

13

Examining the Default Code............................................................................................

16

Creating a Custom Namespace.......................................................................................

16

Adding Summary Comments...........................................................................................

17

Creating the Class............................................................................................................

17

Moving from Code to a Program............................................................................................

19

Compiling Your Program..................................................................................................

20

Looking for Bugs..............................................................................................................

21

Getting Input from the User....................................................................................................

22

Creating a String Variable................................................................................................

24

Getting a Value with the Console.ReadLine() Method.....................................................

24

Incorporating a Variable in Output...................................................................................

25

Combining String Values.......................................................................................................

26

Combining Strings with Concatenation............................................................................

27

Adding a Tab Character...................................................................................................

27

Using the Newline Sequence...........................................................................................

27

Displaying a Backslash....................................................................................................

27

Displaying Quotation Marks.............................................................................................

27

Launching the Mini Adventure...............................................................................................

28

Planning the Story............................................................................................................

28

Creating the Variables......................................................................................................

28

Getting Values from the User...........................................................................................

29

Writing the Output............................................................................................................

29

Finishing the Program......................................................................................................

30

Summary................................................................................................................................

31

Chapter 2: Branching and Operators: The Math Game................................................................

32

The Math Game.....................................................................................................................

32

Using Numeric Variables.......................................................................................................

33

The Simple Math Game...................................................................................................

33

Numeric Variable Types...................................................................................................

34

Integer Variables..............................................................................................................

35

Long Integers...................................................................................................................

36

Floating−Point Variables..................................................................................................

36

Data Type Problems........................................................................................................

37

i

 

Table of Contents

 

Chapter 2: Branching and Operators: The Math Game

 

Math Operators................................................................................................................

37

Converting Variables..............................................................................................................

37

Explicit Casting.................................................................................................................

39

The Convert Object..........................................................................................................

39

Creating a Branch in Program Logic......................................................................................

41

The Hi Bill Game..............................................................................................................

41

Condition Testing.............................................................................................................

43

The If Statement...............................................................................................................

44

The Else Clause...............................................................................................................

44

Multiple Conditions...........................................................................................................

44

Working with The Switch Statement......................................................................................

45

The Switch Demo Program..............................................................................................

45

Examining How Switch Statements Work........................................................................

46

Creating a Random Number..................................................................................................

47

Introducing the Die Roller.................................................................................................

47

Exploring the Random Object..........................................................................................

48

Creating a Random Double with the .NextDouble() Method............................................

48

Getting the Values of Dice...............................................................................................

49

Creating the Math Game........................................................................................................

50

Designing the Game........................................................................................................

50

Creating the Variables......................................................................................................

50

Managing Addition...........................................................................................................

51

Managing Subtraction......................................................................................................

52

Managing Multiplication and Division...............................................................................

52

Checking the Answers.....................................................................................................

53

Waiting for the Carriage Return.......................................................................................

53

Summary................................................................................................................................

54

Chapter 3: Loops and Strings: The Pig Latin Program................................................................

55

Project: The Pig Latin Program..............................................................................................

55

Investigating The String Object..............................................................................................

56

The String Mangler Program............................................................................................

56

A Closer Look at Strings..................................................................................................

56

Using the Object Browser................................................................................................

57

Experimenting with String Methods..................................................................................

58

Performing Common String Manipulations......................................................................

59

Using a For Loop...................................................................................................................

60

Examining The Bean Counter Program...........................................................................

60

Creating a Sentry Variable...............................................................................................

61

Checking for an Upper Limit.............................................................................................

61

Incrementing the Variable................................................................................................

61

Examining the Behavior of the For Loop..........................................................................

61

Varying the For Loop’s Behavior............................................................................................

62

The Fancy Beans Program..............................................................................................

63

Skipping Numbers............................................................................................................

64

Counting Backwards........................................................................................................

64

Using a Foreach Loop to Break Up a Sentence...............................................................

65

Using a While Loop................................................................................................................

65

The Magic Word Program................................................................................................

66

ii

 

Table of Contents

 

Chapter 3: Loops and Strings: The Pig Latin Program

 

Writing an Effective While Loop.......................................................................................

68

Planning Your Program with the STAIR Process...................................................................

70

S: State the Problem........................................................................................................

70

T: Tool Identification.........................................................................................................

70

A: Algorithm......................................................................................................................

71

I: Implementation..............................................................................................................

71

R: Refinement..................................................................................................................

72

Applying STAIR to the Pig Latin Program..............................................................................

72

Stating the Problem..........................................................................................................

73

Identifying the Tools.........................................................................................................

73

Creating the Algorithm.....................................................................................................

73

Implementing and Refining..............................................................................................

74

Writing the Pig Latin Program................................................................................................

74

Setting Up the Variables..................................................................................................

74

Creating the Outside Loop...............................................................................................

75

Dividing the Phrase into Words........................................................................................

75

Extracting the First Character..........................................................................................

76

Checking for a Vowel.......................................................................................................

76

Adding Debugging Code..................................................................................................

76

Closing Up the code.........................................................................................................

77

Summary................................................................................................................................

77

Chapter 4: Objects and Encapsulation: The Critter Program......................................................

78

Introducing the Critter Program..............................................................................................

78

Creating Methods to Reuse Code..........................................................................................

80

The Song Program...........................................................................................................

80

Building the Main() Method..............................................................................................

81

Creating a Simple Method................................................................................................

82

Adding a Parameter.........................................................................................................

83

Returning a Value............................................................................................................

84

Creating a Menu....................................................................................................................

85

Creating a Main Loop.......................................................................................................

85

Creating the Sentry Variable............................................................................................

86

Calling a Method..............................................................................................................

86

Working with the Results..................................................................................................

87

Writing the showMenu() Method......................................................................................

87

Getting Input from the User..............................................................................................

87

Handling Exceptions........................................................................................................

88

Returning a Value............................................................................................................

89

Creating a New Object with the CritterName Program..........................................................

89

Creating the Basic Critter.................................................................................................

89

Using Scope Modifiers.....................................................................................................

90

Using a Public Instance Variable.....................................................................................

90

Creating an Instance of the Critter...................................................................................

91

Referring to the Critter’s Members...................................................................................

91

Adding a Method....................................................................................................................

91

Creating the talk() Method for the CritterTalk Program....................................................

92

Changing the Menu to Use the talk() Method..................................................................

92

Creating a Property in the CritterProp Program.....................................................................

92

iii

 

Table of Contents

 

Chapter 4: Objects and Encapsulation: The Critter Program

 

Examining the Critter Prop Program................................................................................

93

Creating the Critter with a Name Property.......................................................................

93

Using Properties as Filters...............................................................................................

95

Making the Critter More Lifelike.............................................................................................

96

Adding More Private Variables.........................................................................................

96

Adding the Age() Method.................................................................................................

97

Adding the Eat() Method..................................................................................................

97

Adding the Play() Method.................................................................................................

98

Modifying the Talk() Method.............................................................................................

98

Making Changes in the Main Class..................................................................................

98

Summary................................................................................................................................

99

Chapter 5: Constructors, Inheritance, and Polymorphism: The Snowball Fight.....................

101

Introducing the Snowball Fight.............................................................................................

101

Inheritance and Encapsulation.............................................................................................

102

Creating a Constructor.........................................................................................................

102

Adding a Constructor to the Critter Class.......................................................................

103

Creating the CritViewer Class........................................................................................

104

Reviewing the Static Keyword........................................................................................

105

Calling a Constructor from the Main() Method...............................................................

106

Examining CritViewer’s Constructor...............................................................................

106

Working with Multiple Files.............................................................................................

107

Overloading Constructors....................................................................................................

108

Viewing the Improved Critter Class................................................................................

108

Adding Polymorphism to Your Objects..........................................................................

109

Modifying the Critter Viewer in CritOver to Demonstrate Overloaded Constructors......

110

Using Inheritance to Make New Classes.............................................................................

111

Creating a Class to View the Clone...............................................................................

112

Creating the Critter Class...............................................................................................

113

Improving an Existing Class.................................................................................................

113

Introducing the Glitter Critter..........................................................................................

114

Calling the Base Class’s Constructors...........................................................................

115

Adding Methods to a New Class....................................................................................

116

Changing the Critter Viewer Again.................................................................................

116

Using Polymorphism to Alter a Class’s Behavior.................................................................

116

Creating the Snowball Fight.................................................................................................

117

Building the Fighter........................................................................................................

118

Building the Robot Fighter..............................................................................................

120

Creating the Main Menu Class.......................................................................................

122

Summary..............................................................................................................................

125

Chapter 6: Creating a Windows Program: The Visual Critter....................................................

127

Overview..............................................................................................................................

127

Introducing the Visual Critter................................................................................................

127

Creating a Windows−Style Program with a GUI..................................................................

134

Thinking Like a GUI Programmer...................................................................................

135

Creating a Graphical User Interface (GUI).....................................................................

136

Examining the Code of a Windows Program.......................................................................

139

Adding New Namespaces..............................................................................................

140

iv

 

Table of Contents

 

Chapter 6: Creating a Windows Program: The Visual Critter

 

Creating the Form Object...............................................................................................

142

Creating a Destructor.....................................................................................................

143

Creating the Components..............................................................................................

144

Setting Component Properties.......................................................................................

145

Setting Up the Form.......................................................................................................

145

Writing the Main() Method..............................................................................................

146

Creating an Interactive Program..........................................................................................

147

Responding to a Simple Event.......................................................................................

147

Creating and Adding the Components...........................................................................

148

Adding an Event to the Program....................................................................................

148

Creating an Event Handler.............................................................................................

149

Allowing for Multiple Selections...........................................................................................

150

Choosing a Font with Selection Controls.......................................................................

150

Creating the User Interface............................................................................................

151

Examining Selection Tools.............................................................................................

153

Creating Instance Variables in the Font Chooser..........................................................

154

Writing the AssignFont() Method....................................................................................

155

Writing the Event Handlers............................................................................................

157

Working with Images and Scroll Bars..................................................................................

157

Changing an Image’s Size.............................................................................................

158

Setting Up the Picture Box.............................................................................................

159

Adding a Scroll Bar........................................................................................................

161

Writing the Event−Handling Code..................................................................................

161

Revisiting the Visual Critter..................................................................................................

161

Designing the Program..................................................................................................

162

Determining the Necessary Tools..................................................................................

163

Designing the Form........................................................................................................

163

Writing the Code............................................................................................................

164

Summary..............................................................................................................................

167

Chapter 7: Timers and Animation: The Lunar Lander................................................................

168

Introducing the Lunar Lander...............................................................................................

168

Reading Values from the Keyboard.....................................................................................

169

Introducing the Key Reader Program.............................................................................

169

Setting Up the Key Reader Program..............................................................................

171

Coding the KeyPress Event...........................................................................................

171

Coding the KeyDown Event...........................................................................................

173

Determining Which Key Was Pressed...........................................................................

175

Animating Images................................................................................................................

175

Introducing the ImageList Control..................................................................................

176

Setting Up an Image List................................................................................................

177

Looking at the Image Collection.....................................................................................

178

Displaying an Image from the Image List.......................................................................

179

Using a Timer to Automate Animation.................................................................................

180

Introducing the Timer Control.........................................................................................

180

Configuring the Timer.....................................................................................................

181

Adding Motion......................................................................................................................

182

Checking for Keyboard Input..........................................................................................

184

Working with the Location Property...............................................................................

184

v

 

Table of Contents

 

Chapter 7: Timers and Animation: The Lunar Lander

 

Detecting Collisions between Objects..................................................................................

186

Coding the Crasher Program.........................................................................................

188

Getting Values for newX and newY...............................................................................

189

Bouncing the Ball off the Sides......................................................................................

189

Checking for Collisions...................................................................................................

189

Extracting a Rectangle from a Component....................................................................

189

Getting More from the MessageBox Object.........................................................................

190

Introducing the MsgDemo Program...............................................................................

190

Retrieving Values from the MessageBox.......................................................................

192

Coding the Lunar Lander.....................................................................................................

192

The Visual Design..........................................................................................................

192

The Designer−Generated Code.....................................................................................

193

Class−Level Variables...................................................................................................

194

The Constructor.............................................................................................................

195

The timer1_Tick() Method..............................................................................................

195

The moveShip() Method.................................................................................................

196

The checkLanding() Method..........................................................................................

197

The theForm_KeyDown() Method..................................................................................

199

The showStats() Method................................................................................................

200

The killShip() Method.....................................................................................................

200

The initGame() Method..................................................................................................

201

Summary..............................................................................................................................

202

Chapter 8: Arrays: The Soccer Game..........................................................................................

203

The Soccer Game................................................................................................................

203

Introducing Arrays................................................................................................................

204

Exploring the Counter Program......................................................................................

205

Creating an Array of Strings...........................................................................................

207

Referring to Elements in an Array..................................................................................

208

Working with Arrays.............................................................................................................

208

Using the Array Demo Program to Explore Arrays........................................................

208

Building the Languages Array........................................................................................

209

Sorting the Array............................................................................................................

209

Creating Tables with Two−Dimensional Arrays.............................................................

214

Designing the Soccer Game................................................................................................

214

Solving a Subset of the Problem....................................................................................

215

Adding Percentages for the Other Players.....................................................................

216

Setting Up the Shot Demo Program...............................................................................

216

Setting Up the List Boxes...............................................................................................

217

Using a Custom Event Handler......................................................................................

218

Writing the changeStatus() Method................................................................................

219

Kicking the Ball...............................................................................................................

219

Designing Programs by Hand..............................................................................................

220

Examining the Form by Hand Program..........................................................................

220

Adding Components in the Constructor.........................................................................

221

Responding to the Button Event....................................................................................

222

Building the Soccer Program...............................................................................................

222

Setting Up the Variables................................................................................................

222

Examining the Constructor.............................................................................................

225

vi

 

Table of Contents

 

Chapter 8: Arrays: The Soccer Game

 

Setting Up the Players...................................................................................................

225

Setting Up the Opponents..............................................................................................

227

Setting Up the Goalies...................................................................................................

228

Responding to Player Clicks..........................................................................................

228

Handling Good Shots.....................................................................................................

229

Handling Bad Shots.......................................................................................................

230

Setting a New Current Player.........................................................................................

230

Handling the Passage of Time.......................................................................................

231

Updating the Score........................................................................................................

234

Summary..............................................................................................................................

235

Chapter 9: File Handling: The Adventure Kit..............................................................................

236

Introducing the Adventure Kit...............................................................................................

236

Viewing the Main Screen...............................................................................................

236

Loading an Adventure....................................................................................................

237

Playing an Adventure.....................................................................................................

238

Creating an Adventure...................................................................................................

240

Reading and Writing Text Files............................................................................................

241

Exploring the File IO Program........................................................................................

242

Importing the IO Namespace.........................................................................................

242

Writing to a Stream........................................................................................................

243

Reading from a Stream..................................................................................................

244

Creating Menus....................................................................................................................

245

Exploring the Menu Demo Program...............................................................................

245

Adding a MainMenu Object............................................................................................

246

Adding a Submenu.........................................................................................................

247

Setting Up the Properties of Menu Items.......................................................................

248

Writing Event Code for Menus.......................................................................................

249

Using Dialog Boxes to Enhance Your Programs.................................................................

250

Exploring the Dialog Demo Program..............................................................................

250

Adding Standard Dialogs to Your Form.........................................................................

253

Using the File Dialog Controls........................................................................................

253

Responding to File Dialog Events..................................................................................

254

Using the Font Dialog Control........................................................................................

255

Using the Color Dialog Control.......................................................................................

256

Storing Entire Objects with Serialization..............................................................................

256

Exploring the Serialization Demo Program....................................................................

256

Creating the Contact Class............................................................................................

257

Referencing the Serializable Namespace......................................................................

258

Storing a Class...............................................................................................................

258

Retrieving a Class..........................................................................................................

259

Returning to the Adventure Kit Program..............................................................................

259

Examining the Room Class............................................................................................

260

Creating the Dungeon Class..........................................................................................

263

Writing the Game Class.................................................................................................

264

Writing the Editor Class..................................................................................................

269

Writing the MainForm Class...........................................................................................

274

Summary..............................................................................................................................

276

vii

Table of Contents

 

Chapter 10: Chapter Basic XML: The Quiz Maker......................................................................

277

Introducing the Quiz Maker Game.......................................................................................

277

Taking a Quiz.................................................................................................................

277

Creating and Editing Quizzes.........................................................................................

278

Investigating XML................................................................................................................

278

Defining XML..................................................................................................................

279

Creating an XML Document in .NET..............................................................................

283

Creating an XML Schema for Your Language...............................................................

284

Investigating the .NET View of XML...............................................................................

285

Exploring the XmlNode Class........................................................................................

285

Exploring the XmlDocument Class.................................................................................

286

Reading an Existing XML Document...................................................................................

287

Creating the XML Viewer Program................................................................................

293

Writing New Values to an XML Document...........................................................................

298

Designating the Class−Level Variables.........................................................................

298

Building the Document Structure...................................................................................

299

Adding an Element to the Document.............................................................................

300

Displaying the XML Code...............................................................................................

301

Examining the Quizzer Program..........................................................................................

302

Building the Main Form..................................................................................................

303

Writing the Quiz Form....................................................................................................

304

Writing the Editor Form..................................................................................................

310

Summary..............................................................................................................................

317

Chapter 11: Databases and ADO.NET: The Spy Database........................................................

318

Overview..............................................................................................................................

318

Introducing the SpyMaster Program....................................................................................

318

Creating a Simple Database................................................................................................

321

Accessing the Data Server.............................................................................................

321

Accessing the Data in a Program...................................................................................

326

Using Queries to Modify Data Results.................................................................................

333

Limiting Data with the SELECT Statement....................................................................

333

Using an Existing Database...........................................................................................

338

Adding the Capability to Display Queries.......................................................................

339

Creating a Visual Query Builder.....................................................................................

340

Working with Relational Databases.....................................................................................

345

Improving Your Data with Normalization........................................................................

346

Using a Join to Connect Two Tables.............................................................................

347

Creating a View..............................................................................................................

350

Referring to a View in a Program...................................................................................

353

Incorporating the Agent Specialty Attribute....................................................................

353

Working with Other Databases............................................................................................

355

Creating a New Connection...........................................................................................

355

Converting a Data Set to XML.......................................................................................

359

Reading from XML to a Data Source.............................................................................

360

Creating the SpyMaster Database.......................................................................................

361

Building the Main Form..................................................................................................

361

Editing the Assignments.................................................................................................

362

Editing the Specialties....................................................................................................

363

Viewing the Agents........................................................................................................

364

viii

 

Table of Contents

 

Chapter 11: Databases and ADO.NET: The Spy Database

 

Editing the Agent Data...................................................................................................

365

Summary..............................................................................................................................

374

List of Figures................................................................................................................................

375

List of Tables..................................................................................................................................

382

List of Sidebars..............................................................................................................................

383

ix

Соседние файлы в предмете Программирование на C++