Вы находитесь на странице: 1из 12

PREFACE

Approach

This tutorial follows an "objects first" approach. Windows applications are developed from scratch.
Concepts such as objects, classes, properties, events, methods are introduced prior to variables, types,
operators, operands and assignments. Programming for the console window and control structures such
as selection and iteration are introduced even later. This is done in the belief that students learn better if
they are allowed to discover rather than being taught.

Furthermore, this tutorial follows a minimalistic approach. Students should take every small bit of
information as very important as not-so-important material has simply not been included in the text.
Students and lecturers are advised to consult other texts as well.

This is a tutorial and not a text book. This means that students are expected to learn by doing. Instead
of reading through a text book, students are expected to sit in front of a computer and enter the
examples as given. Students should do all of the exercises at the end of every chapter. The exercises
were carefully selected to be representative of essential learning material. They do not only serve as
revision or assessment of existing knowledge but also as medium through which new knowledge should
be discovered. If you miss out on an exercise, you might miss out on some important learning material.
You will not become a good tennis player by watching Wimbledon on TV – you have to go out there and
hit the ball.

Key to fonts and layout

Every topic starts with a short introduction followed by a hands-on do-it-yourself section. A topic is
concluded with an Understand what you are doing section where important concepts are discussed and
explained with reference to the example. Concepts are bulleted on two levels to enable students to focus
on a specific concept at a time.

The key learning outcomes are listed at the beginning of every chapter and every chapter is concluded
with a section that lists some keywords that was discussed in the chapter as well as a set of exercises.

The following convention is followed with regard to font and typeface:

Concepts : Normal
Files and folders : Normal between quotes, e.g. "frmLogin.cs".
Classes and Controls : Green, e.g. Color, TextBox
Properties : Bold, e.g. List
Property values : As it appears in code, e.g. "John", 10, CenterScreen.
Methods : Bold with brackets, e.g. Remove()
Events : Bold with the word "event", e.g. DataError event
Event handlers : As for methods, e.g. Click().
Reserved words : Blue, e.g. new

It is important that the formulations with regard to objects and classes are understood correctly. For
example, "a TextBox object" refers to an object or specific instance of the TextBox class. Reference to
methods can refer to either static or non-static methods, unless specifically indicated.
Table of Contents
Part 1
1. Controls and Code

2. Drawing Figures

3. Handling Data

4. Console Applications

5. Structure of a C# program

6. Taking Decisions

7. Debugging and Defensive Programming

8. Methods and Parameters

9. Repeating things

10. Collections, Strings and Arrays

Part 2
11. Class development

12. Advanced OO concepts

13. File management

14. Database access

Appendices

A. Naming conventions

B. Glossary
ii

Chapter 1: Controls and Code

Getting started 1-2


Start Visual Studio 2008 and create a new project 1-2
Rename a form 1-3
Run the application 1-4
Change the form's properties 1-5
Save, Close and Reopen your work 1-6
Reference 1-6

Write your first C# code 1-7


Add a button to the form 1-7
Write the code 1-8

Add controls from the toolbox 1-11

Naming conventions 1-15

Comments 1-16

Traffic light example 1-17

Resources 1-19

Keywords 1-19

Exercises 1-20

Chapter 2: Drawing Figures

The Graphics class 2-2

Keywords 2-6

Exercises 2-6
iii

Chapter 3: Handling Data

Data representation 3-2


Bits and Bytes 3-2
Representation of bool values 3-2
Number systems 3-2
Representation of characters 3-3

Understanding variables and types 3-4


Primitive types 3-4
Assignment of values 3-5
An example 3-6
Type casts 3-8
The difference between 9 and '9' 3-10

Arithmetic operators and operands 3-10


List of operators 3-10
Operator precedence 3-12
Integer division and the modulus operator 3-12

Formatting output 3-14


The ToString() method 3-14
The Format() method 3-15

Keywords 3-16

Exercises 3-17

Chapter 4: Console Applications

"Hello World!" again 4-2

Producing output 4-3

Accepting input 4-4

Keywords 4-6

Exercises 4-6
iv

Chapter 5: Structure of a C# Program

The role of Visual Studio 5-2

The basic structure of a C# program 5-3

Namespaces 5-4

The file structure of a C# program 5-5

Keywords 5-7

Exercise 5-7

Chapter 6: Taking Decisions

Basic if statements 6-2

Program design 6-4


Understand the problem 6-4
Design the solution 6-4
Implement the solution 6-6
Test the application 6-6

Relational operators 6-6

Nested if statements 6-9


Boolean operators and compound conditions 6-12
The logical AND 6-12
The logical OR 6-13
The logical NOT 6-15
Order of precedence 6-16

The scope of variables 6-17


The switch statement 6-18
The conditional operator 6-20
Keywords 6-21
Exercises 6-22
v

Chapter 7: Debugging and Defensive Programming

Errors 7-2
Debugging 7-4
Defensive programming 7-5
Keywords 7-9
Exercises 7-9

Chapter 8: Methods and Parameters

Some revision 8-2


Method definition 8-3
Methods that return a value 8-6
Methods that need information 8-7
Advantages of using methods 8-9
An example 8-9

Another way of communication 8-12


Overloaded methods 8-14
Defensive programming revisited 8-15
Keywords 8-17
Exercises 8-17
vi

Chapter 9: Repeating things

Basic loops 9-2


Types of loops 9-5
Counter-controlled loops 9-5
Sentinel-controlled loops 9-5
State-controlled loops 9-11

The for loop 9-15


do…while 9-17
An example: Squares and square roots 9-17
An example: A menu for a console application 9-19

Nested loops 9-21


Example 1 9-22
Example 2 9-22
Example 3 9-23

Recursion 9-26
Keywords 9-28
Exercises 9-29
vii

Chapter 10: Collections, Strings and Arrays


Collections 10-2
Using a list box to store several values 10-2
Add items to a list box 10-2
Step through items in a list box 10-3
Using foreach to step through a collection 10-4
Stepping through items in a list box - again 10-5
Using a loop to accumulate totals 10-6
Print data from a list box 10-7
Character strings 10-8
Comparison of string 10-8
Basic string methods: IndexOf(), Substring() and ToUpper() 10-10
Step through a string 10-12
Remove characters from a string 10-14
Summary of string methods 10-17

Arrays 10-18
Array basics 10-18
Declaration, instantiation, initialisation, traversing and sorting 10-18
A Windows Forms example 10-20
- Scope of an array, Entering data, Listing elements 10-20
- Add an additional element 10-23
- Remove an element 10-23
- Remove duplicates 10-24
- Insert an element into the middle of an array 10-24
- The smallest / largest element in an array 10-25
- The average of numbers in an array 10-26

Two-dimensional arrays 10-27


Hard-coded initialisation 10-27
User-defined content 10-28
- Arrays as parameters 10-29
- Parallel arrays 10-31

Jagged arrays 10-32


The ArrayList class 10-34
Resources 10-38
Keywords 10-39
Exercises 10-40
viii

Chapter 11: Class development

OO Basics – Some revision 11-2


Develop and implement your own class 11-3
Add a new class 11-3
Implement the class 11-6
Return an object from a method 11-9
Tips and tricks for quicker development 11-10
Business rules and defensive programming 11-12
Constructors 11-14
Object initialisers 11-16
Static methods and properties 11-17
Instance methods 11-18
Predicate methods 11-19
Sort the objects in an array list 11-20
Allow users to control the sorting 11-21
Keywords 11-23
Exercises 11-23

Chapter 12: Advanced OO concepts

Set the scene 12-2


Inheritance 12-4
UML Class diagrams 12-4
Implementation 12-5
Definition of a base class 12-5
Definition of sub-classes 12-8
Implementation of the classes 12-10
Use a separate form to display object details 12-11
Definitions 12-13
Keywords 12-15
Exercises 12-15
ix

Chapter 13: File management

Introduction 13-2
File management 13-2
OpenFileDialog 13-2
File attributes 13-3
File dates 13-3
Delete a file 13-4
Copy a file 13-4
List files in a directory 13-5
Text files 13-6
Saving to and reading from a text file 13-6
Allow the user to select a file and check that it exists 13-9
Binary files 13-11
Save to a binary file 13-11
Read from a binary file 13-13
Save an object to a binary file 13-14
Read an object from a binary file 13-15
Save an array of objects to a binary file 13-16
Read an array of objects from a binary file 13-18
Keywords 13-19
Exercises 13-20
x

Chapter 14: Database access

Database basics 14-2


Database system 14-2
Entities and Relationships 14-2
Create a database in Microsoft Access 14-4
Create the tables 14-4
Relationships and referential integrity 14-4
Queries and SQL 14-6
A single table query 14-6
Join tables together 14-7
Using aggregate functions 14-8
Data access with Visual Studio and C# 14-8
The connection between a data source and a data set 14-8
Display data in a grid 14-9
Update data from a grid 14-12
Use a typed DataSet 14-13
Use a BindingSource 14-17
Table adapters 14-17
Additional functionality 14-19
Set up the application structure 14-19
Anticipate user mistakes 14-20
Currency management 14-21
Sort the data 14-21
Navigate through the data set 14-21
Find a record 14-22
Filter the data 14-23
Parent-Child relationships 14-23
Data-bound text boxes 14-25
Reporting by means of a DbDataReader 14-26
User-defined reports 14-28
Keywords 14-30
Exercises 14-31
xi

Appendix A: Naming conventions

Why having rules? A-2


Conventions A-2
Camel case and Pascal case A-2
Hungarian notation A-2
Solutions, Projects and Forms A-3
Data fields, Methods, Properties A-3
Variables, Parameters and Controls A-3
Variables A-3
Arrays A-3
Constants A-4
Parameters A-4
Controls A-4
Classes, Enums, Structs, Interfaces A-5

Appendix B: Glossary

Hardware B-2
Software B-7

Вам также может понравиться