Table Of ContentVISUAL C#82005
DEMYSTIFIED
JEFF KENT
McGraw-Hill
New York Chicago San Francisco Lisbon London
Madrid Mexico City Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
Copyright © 2006 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as
permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form
or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher.
0-07-148676-3
The material in this eBook also appears in the print version of this title: 0-07-226170-6.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trade-
marked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringe-
ment of the trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate
training programs. For more information, please contact George Hoare, Special Sales, at [email protected] or (212)
904-4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the
work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and
retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works
based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior con-
sent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right
to use the work may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES
AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE
WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTH-
ERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licen-
sors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be
uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or
omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the con-
tent of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any
indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even
if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause what-
soever whether such claim or cause arises in contract, tort or otherwise.
DOI: 10.1036/0072261706
Professional
Want to learn more?
We hope you enjoy this
McGraw-Hill eBook! If
you’d like more information about this book,
its author, or related books and websites,
please click here.
I would like to dedicate this book to the two most
important women in my life, my mom, Beatrice
Baumgarten Kent, who gave up her career as
a chemist for the even more important career of
a mom; and my wife, Devvie Schneider Kent, who,
in addition to being my best friend and lover, also
is the #1 expert in C# at our home.
ABOUT THE AUTHOR
Jeff Kent is an Associate Professor of Computer Science at Los Angeles Valley
College in Valley Glen, California. He teaches a number of programming languages,
including C#, Visual Basic, C++, Java, and—when he's feeling masochistic—
Assembler. He also manages a network for a Los Angeles law firm whose employees
are guinea pigs for his applications, and as an attorney gives advice to young
attorneys whether they want to hear it or not. He also has written several books on
computer programming, recently Visual Basic.NET: A Beginner's Guide and C++
Demystified for McGraw-Hill, and wrote Visual Basic 2005 Demy stifled concurrently
with this book.
Jeff has had a varied career—or careers. He graduated from UCLA with a Bachelor
of Science degree in economics and then went on to obtain a Juris Doctor degree
from Loyola (Los Angeles) School of Law and to practice law. During this time,
when personal computers were still a gleam in Bill Gates's eye, Jeff was also
a professional chess master, earning a third place finish in the United States Under-21
Championship and, later, an international title.
Jeff does find time to spend with his wife, Devvie, which is not difficult since she
is also a computer science professor at Valley College. In addition to his other
career pursuits, he has a part-time job as personal chauffeur for his teenage daughter
Emily (his older daughter Elise now has her own driver's license), and what little
spare time he has, he enjoys watching international chess tournaments on the
Internet. His goal is to resume running marathons, since otherwise, given his losing
battle to lose weight, his next book may be Sumo Wrestling Demystified.
Copyright © 2006 by The McGraw-Hill Companies. Click here for terms of use.
For more information about this title, click here
CONTENTS
Acknowledgments xv
Introduction xvii
CHAPTER 1 Getting Started with Your First
Windows Program 1
Obtaining and Installing Visual C# 2005 2
System Requirements 2
Choosing the Right Version 3
Installing Visual C# 2005 4
Starting Your First Visual C# 2005 Project 4
Starting the Program 5
Specifying the Type of New Project 6
Specifying the Name and Location
of the Project 7
Integrated Development Environment (IDE) 8
Run the Project! 9
What Is a Computer Program? 11
What Is a Programming Language? 13
Translating the Code for the Computer 14
Conclusion 14
Quiz 15
v
Visual C# 2005 Demystified
VI
CHAPTER 2 Writing Your First Code 17
Starting an Existing Project 18
Design View and Code View 20
Object Browser 20
Classes and Objects 25
Inherits 25
Namespaces 25
.NET Framework 26
Properties 27
Properties Window 27
Changing Properties at Design Time 28
What Is a Windows Application? 29
Windows Applications Are Gooey 30
Windows Applications Are Event-Driven 33
Classes Have Events 34
Creating an Event Procedure 34
Creating an Event Procedure Stub 35
Writing Code Inside the Event Procedure 37
Conclusion 40
Quiz 41
CHAPTER 3 Controls 43
Adding Controls to the Form 44
Toolbox 44
Copying a Control from the Toolbox
to the Form 45
Changing the Control's Location 47
Changing the Control's Size 49
Important Label Properties 50
Text Property 50
Name Property 51
The Label Control in Action 52
Mouse Coordinates 52
Creating the Application 53
CONTENTS
Vll
How the Code Works 54
Using Event Procedure Parameters 56
What If You Type the Wrong Code? 56
ToString Method 58
Delegate 59
Conclusion 60
Quiz 61
CHAPTER 4 Storing Information—Data Types
and Variables 63
Data Types 64
Numeric Data Types 65
Text Data Types 65
Data Types of Visual C# Properties 66
Variables 68
Declaring a Variable 68
Where Do I Declare a Variable? 71
Constants 74
Declaring a Constant 75
Why Use Constants? 76
Conclusion 77
Quiz 78
CHAPTER 5 Letting the Program Do the Math—
Arithmetic Operators 79
Arithmetic Operators 80
The Addition Operator 80
The Subtraction Operator 81
The Multiplication Operator 81
The Division Operators 81
Operator Precedence 82
Combining Arithmetic
and Assignment Operators 83
Increment and Decrement Operators 84
Visual C# 2005 Demystified
viii
The Parse Method 85
Class Methods 86
Change Machine Project 86
Creating the Project 87
The Algorithm 89
Conclusion 91
Quiz 92
CHAPTER 6 Making Comparisons—Comparison
and Logical Operators 93
Debugging 94
Comparison Operators 96
Numeric Comparison Operators 96
String Comparisons 98
Precedence 99
Logical Operators 99
The && Operator 100
The & Operator 101
The || Operator 101
The | Operator 102
The A Operator 102
The ! Operator 103
Precedence 104
Why && and || in Addition to & and 104
Conclusion 105
Quiz 105
CHAPTER 7 Making Choices—if and switch
Control Structures 107
Creating a Test Project 108
The if Control Structure 108
The if Statement 109
The if ... else Statement 113
The if ... else if Statement 116