xxix
INTRODUCTION
GETTING THE MOST OUT OF THE BOOK
This book provides a lot of tools that you can use to best match your learning style
but you have to use them. If you learn best by reading text, spend more time on the
text. If you like step-by-step instructions, focus on the Try It’s step-by-step exercise.
If you learn best by watching and listening, focus on the screencasts.
Then, after you’ve finished a lesson, use the exercises to verify that you’ve mastered
the material.
And dont be afraid to invent programs of your own. Just because an idea isn’t in
the book doesn’t mean it wouldn’t make good practice.
HOW THIS BOOK IS STRUCTURED
This book is divided into six sections, each containing a series of short lessons. The lessons are gen-
erally arranged in order with later lessons depending on earlier ones, so you should study the lessons
more or less in order, at least until sections V and VI. The lessons in sections V and VI cover slightly
more specialized topics and their order is less critical. (However, you should still probably study
“Programming Databases, Part 1” before you study “Programming Databases, Part 2,” but you can
study them before or after reading the lesson on printing.)
PERSISTENT PROGRAMS
A number of the lessons work with the SimpleEdit example program. This pro-
gram starts as a simple form containing a few controls in Lesson 3 and grows into
a serviceable word processing application before all is done.
Each time a lesson asks you to add to this program, it tells you to copy the previous
version. If you skip a step, you may not have the necessary version available. In that
case, you can download the version you need from the book’s web site.
For example, the instructions for Lesson 6’s Exercise 1 say to copy the version you
built for Lesson 5, Exercise 5. If you skipped that exercise, you can download the
Lesson 5 material from the books web site and use the version of SimpleEdit that
it contains.
Section I: The Visual Studio IDE and Controls
The lessons in this section explain how to use the Visual Studio integrated development environ-
ment (IDE) and how to use the controls that make up a user interface. These form the foundation on
which you build everything else in a Visual C# program.
596906flast.indd 29 4/7/10 12:31:16 PM
xxx
INTRODUCTION
Lesson 1, “Getting Started with the Visual Studio IDE, explains Visual Studio. It describes some
of the IDE’s useful features and shows how to build a simple program and run it. From the very first
lesson, you’ll be able to build a program!
Lesson 2, “Creating Controls, explains what controls are, what they are for, and how to use them.
It shows how to add controls to a form to make a user interface. In Visual C# programs, controls
are critical for getting the job done.
Lesson 3, “Making Controls Arrange Themselves, explains how to use control properties to make
controls automatically rearrange themselves at run time to take full advantage of the available space.
When the user resizes a form, the controls can automatically move and resize as needed.
Lesson 4, “Handling Events, explains what events are. It shows how to catch events to respond to
user actions so the user can interact with the program.
Lesson 5, “Making Menus, explains how to add main menus and context menus to an application,
and how to respond when the user selects a menu item. By responding to menu events, the program
gives the user an easy and well-understood way to control the application.
Lesson 6, “Making Tool Strips and Status Strips, explains how to build tool strips and status strips,
and how to handle their events. Tool strips provide a faster method for the user to control the appli-
cation than menus, while status strips provide useful feedback to let the user know what the program
is doing.
Lesson 7, “Using RichTextBoxes, explains the
RichTextBox control and shows how to manipulate
it with code. The
RichTextBox allows the user to enter text and, if the program provides the right
tools, lets the user format the text with different fonts, colors, bullets, and other text decorations.
Lesson 8, “Using Standard Dialogs,explains standard dialogs. It shows how to use them to display
messages and questions, change fonts and colors, browse for folders, and let the user select files for
opening and saving.
Lesson 9, “Creating and Displaying New Forms, explains how code can display new instances of
forms and interact with the controls on those forms. This is important for more complicated programs
that cannot do everything they need to on a single form.
Lesson 10, “Building Custom Dialogs, explains how to use new forms as custom dialogs. It shows
how to use the
Form object’s ShowDialog method, how to assign a dialog’s return result, and how to
interpret that result.
Section II: Variables and Calculations
The lessons in this section deal with variables and calculations. They explain what variables are
and how a program can use them to calculate results. The lessons in Section I explain how to make
controls that let the user enter information. The lessons in this section explain how to take that
information and do something with it.
Lesson 11, “Using Variables and Performing Calculations, explains how to declare and use variables
and constants, and how to perform simple calculations. It also shows how to convert information
596906flast.indd 30 4/7/10 12:31:16 PM
xxxi
INTRODUCTION
from one data type to another. For example, it shows how to take an age entered by the user and con-
vert it from the textual value “47” into the numeric value 47. (Its a small distinction to a person but a
huge one to a program.)
Lesson 12, “Debugging Code, explains techniques for finding and fixing bugs. It shows how to set
breakpoints in the code, step into and over routines, examine and modify variable values, and use
watches. Almost every nontrivial program starts with a bug or two. This lesson shows how to find
those bugs.
Lesson 13, “Understanding Scope, explains how scope restricts a variable’s accessibility to pieces
of code. It also explains why a programmer should restrict scope as much as possible.
Lesson 14, “Working with Strings, explains how to combine, manipulate, and format strings. It
explains the
String class’s ToString and Format methods that let you build nicely formatted string
to show the user.
Lesson 15, “Working with Dates and Times, explains how to use date and time values. It explains
the
Date and TimeSpan classes, and shows how to use and format them.
Lesson 16, “Using Arrays and Collections, explains single- and multi-dimensional arrays and
collection classes such as
Collection and ArrayList. It explains how to declare and initialize
arrays and collections.
Lesson 17, “Using Enumerations and Structures, explains how to use more advanced data types such
as structures and enumerations. These help make the code easier to understand, debug, and maintain.
Section III: Program Statements
The lessons in the previous sections explain how to let the user control the program by raising
events, to get data entered by the user in controls, to place that data in variables, and to perform
simple calculations. The lessons in this section explain how to perform more complex calculations.
They explain how to control the program’s flow, make decisions, and repeat operations. While con-
trols and message boxes are some of the more visible pieces of an application, these statements let
the program do most of its work.
Lesson 18, “Making Choices, explains how programmers can control the flow of code with
if,
switch, nested if, and cascading if statements. These statements let the program take different
actions depending on the situation.
Lesson 19, “Repeating Program Steps,explains looping code that uses
for, foreach, do, and while
statements. It explains how to use these statements to iterate through a series of integer values, arrays,
and lists, and how to break out of loops.
Lesson 20, “Reusing Code with Methods, explains how a programmer can write functions and
why functions are important. It explains the syntax of declaring functions, defining return values,
and using parameters passed by value or reference.
Lesson 21, “Handling Errors, explains how to use
try blocks to handle unexpected errors. It also
explains how to throw errors to tell other parts of the program that something has gone wrong.
596906flast.indd 31 4/7/10 12:31:16 PM
xxxii
INTRODUCTION
Lesson 22, “Preventing Bugs, explains bug proofing techniques that make it easier to detect and
correct bugs. It explains how to use
Assert statements to validate inputs and results so you can
catch bugs quickly rather than letting them remain hidden in the code.
Section IV: Classes
Structures, enumerations, and functions are all programming abstractions that let you think about
pieces of the program at a higher level. When you call the
CalculateInterest function, you don’t
need to know how it works, just that it does.
The ultimate programming abstraction is the class. A class lets you think about data and functions
packaged as a single unit. For example, a
Customer class might include data (name, employee ID,
office number) together with functions (
ScheduleWork, PrintPaycheck).
The lessons in this section deal with classes. They explain how to create and use classes and how to
use more advanced class features such as generics and operator overloading.
Lesson 23, “Defining Classes, explains how to define classes. It explains the three fundamental
characteristics of object-oriented classes (inheritance, encapsulation, and polymorphism) and how
classes provide them. It explains how to build simple properties and methods.
Lesson 24, “Initializing Objects, explains constructors, destructors, and initializers, and shows
how to use them to make creating objects easier.
Lesson 25, “Fine-Tuning Classes, explains how you can overload and override class methods. These
techniques let you make classes more flexible and easier to use.
Lesson 26, “Overloading Operators, explains operator overloading. This technique lets you define
the behavior of operators such as +, *, and % for objects other than numbers.
Lesson 27, “Using Interfaces, explains what a class interface is and how to build one. Just as a
program’s user interface defines the features that a user sees, a class interface defines the features
that a class must have to implement the interface.
Lesson 28, “Making Generic Classes,explains how to build new generic classes. Whereas Lesson 16
shows how to use generic collection classes such as
Hashtable to work with different kinds of data,
this lesson explains how you can build your own generic classes and methods.
Section V: System Interactions
Earlier lessons explain how to let a program interact with the user. The lessons in this section
explain methods a program can use to interact with the operating system and other programs.
Lesson 29, “Reading and Writing Files, explains how a program can read and write files. It
explains how to use streams to manipulate the text in a file all at once or in pieces, for example,
one line at a time.
Lesson 30, “Using File System Classes, explains ways in which a program can use classes to find,
examine, and manipulate directories and files. It describes file handling classes such as
DriveInfo,
DirectoryInfo, Directory, and FileInfo.
596906flast.indd 32 4/7/10 12:31:17 PM
xxxiii
INTRODUCTION
Lesson 31, “Printing, explains how to create printouts. It tells how to draw simple shapes and text
on one or more pages sent to the printer.
Lesson 32, “Using the Clipboard, explains how to move text, images, and file drop lists in and
out of the clipboard. Using the clipboard in this way is somewhat crude, but it’s simple and flexible,
allowing your program to interact with many others without understanding anything about how
those other applications work.
Lesson 33, “Providing Drag and Drop, explains how a program can use drag-and-drop to interact
with other programs. It explains how to start a drag, provide “drag over” feedback, and handle a
drop. Like the clipboard, drag-and-drop lets your programs interact with others without knowing
how the other programs work.
Section VI: Specialized Topics
The lessons presented in the earlier sections cover topics that are generally useful for a large variety of
programs. Whether you are writing a sales tax calculator, an invoice tracking system, or a word guess-
ing game, techniques such as handling events, debugging code, using
if statements, and printing will
be useful to you.
This section introduces topics that are more specialized so you might not need them in every program
you write. For example, localizing your application for different locales is important for some appli-
cations but not for every program you write.
Each of the lessons in this section gives a sense of what its topic is about and provides enough detail
to get started, but the lessons cannot cover their topics in complete detail due to the topic’s length
and complexity.
Lesson 34, “Localizing Programs, explains how to make programs that can run in multiple cultures.
It shows how to use different text and images for different locales and discusses some of the other
issues an international application must address.
Lesson 35, “Programming Databases, Part 1, explains how to use Visual Studio wizards to build
a simple database application quickly. It shows how to display the data in a database and save any
changes that the user makes.
Lesson 36, “Programming Databases, Part 2, explains slightly more advanced database features
than those covered by Lesson 35. It explains how to make the simple programs described in Lesson 35
search, filter, and sort their results.
Lesson 37, “LINQ to Objects, explains how you can use Language-Integrated Query (LINQ) to
lter and extract values from collections and other enumerable lists into new ones. This lets you
perform database-like queries on data stored in program objects instead of an actual database.
Lesson 38, “LINQ to SQL, explains how you can use LINQ to manipulate database entities as if they
were objects inside the program. It lets your program treat records in tables as if they were instances of
classes, and provides an alternative database strategy to the one used in Lessons 35 and 36.
Lesson 39, “Drawing with GDI+, provides more detail about drawing with Graphic Device
Interface (GDI+) functions. Previous lessons, notably Lesson 31, used GDI+ functions to draw.
596906flast.indd 33 4/7/10 12:31:17 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123