
- •Contents
- •What Is C#?
- •C# Versus Other Programming Languages
- •Preparing to Program
- •The Program Development Cycle
- •Your First C# Program
- •Types of C# Programs
- •Summary
- •Workshop
- •C# Applications
- •Basic Parts of a C# Application
- •Structure of a C# Application
- •Analysis of Listing 2.1
- •Object-Oriented Programming (OOP)
- •Displaying Basic Information
- •Summary
- •Workshop
- •Variables
- •Using Variables
- •Understanding Your Computer’s Memory
- •C# Data Types
- •Numeric Variable Types
- •Literals Versus Variables
- •Constants
- •Reference Types
- •Summary
- •Workshop
- •Types of Operators
- •Punctuators
- •The Basic Assignment Operator
- •Mathematical/Arithmetic Operators
- •Relational Operators
- •Logical Bitwise Operators
- •Type Operators
- •The sizeof Operator
- •The Conditional Operator
- •Understanding Operator Precedence
- •Converting Data Types
- •Understanding Operator Promotion
- •For Those Brave Enough
- •Summary
- •Workshop
- •Controlling Program Flow
- •Using Selection Statements
- •Using Iteration Statements
- •Using goto
- •Nesting Flow
- •Summary
- •Workshop
- •Introduction
- •Abstraction and Encapsulation
- •An Interactive Hello World! Program
- •Basic Elements of Hello.cs
- •A Few Fundamental Observations
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Essential Elements of SimpleCalculator.cs
- •A Closer Look at SimpleCalculator.cs
- •Simplifying Your Code with Methods
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Lexical Structure
- •Some Thoughts on Elevator Simulations
- •Concepts, Goals and Solutions in an Elevator Simulation Program: Collecting Valuable Statistics for Evaluating an Elevator System
- •A Deeper Analysis of SimpleElevatorSimulation.cs
- •Class Relationships and UML
- •Summary
- •Review Questions
- •Programming Exercises
- •The Hello Windows Forms Application
- •Creating and Using an Event Handler
- •Defining the Border Style of the Form
- •Adding a Menu
- •Adding a Menu Shortcut
- •Handling Events from Menus
- •Dialogs
- •Creating Dialogs
- •Using Controls
- •Data Binding Strategies
- •Data Binding Sources
- •Simple Binding
- •Simple Binding to a DataSet
- •Complex Binding of Controls to Data
- •Binding Controls to Databases Using ADO.NET
- •Creating a Database Viewer with Visual Studio and ADO.NET
- •Resources in .NET
- •Localization Nuts and Bolts
- •.NET Resource Management Classes
- •Creating Text Resources
- •Using Visual Studio.NET for Internationalization
- •Image Resources
- •Using Image Lists
- •Programmatic Access to Resources
- •Reading and Writing RESX XML Files
- •The Basic Principles of GDI+
- •The Graphics Object
- •Graphics Coordinates
- •Drawing Lines and Simple Shapes
- •Using Gradient Pens and Brushes
- •Textured Pens and Brushes
- •Tidying up Your Lines with Endcaps
- •Curves and Paths
- •The GraphicsPath Object
- •Clipping with Paths and Regions
- •Transformations
- •Alpha Blending
- •Alpha Blending of Images
- •Other Color Space Manipulations
- •Using the Properties and Property Attributes
- •Demonstration Application: FormPaint.exe
- •Why Use Web Services?
- •Implementing Your First Web Service
- •Testing the Web Service
- •Implementing the Web Service Client
- •Understanding How Web Services Work
- •Summary
- •Workshop
- •How Do Web References Work?
- •What Is UDDI?
- •Summary
- •Workshop
- •Passing Parameters and Web Services
- •Accessing Data with Web Services
- •Summary
- •Workshop
- •Managing State in Web Services
- •Dealing with Slow Services
- •Workshop
- •Creating New Threads
- •Synchronization
- •Summary
- •The String Class
- •The StringBuilder Class
- •String Formatting
- •Regular Expressions
- •Summary
- •Discovering Program Information
- •Dynamically Activating Code
- •Reflection.Emit
- •Summary
- •Simple Debugging
- •Conditional Debugging
- •Runtime Tracing
- •Making Assertions
- •Summary
Какую работу нужно написать?

Chapter 5 • YOUR FIRST OBJECT-ORIENTED C# PROGRAM 137
Other example of associations that are not aggregations are
•Employee works for Company
•BankCustomer interacts with the BankTeller
Note
Associations that precisely connect two classes are called binary associations; they are the most common kind of association.
Summary
This chapter consists of two main parts. The first part is about the lexical structure of a C# source program. The second part provides an example of an object-oriented program, which relates directly to the discussion in Chapter 3 about abstraction and encapsulation.
The following are the most important points covered in this chapter:
A C# source program can be viewed as a collection of identifiers, keywords, whitespace, comments, literals, operators, and separators.
C# is a case-sensitive language. To improve the clarity for other readers of the code, it is important to adhere to a certain capitalization style. Pascal Casing (ThisIsPascalCasing) and Camel Casing (thisIsCamelCasing) are the preferred styles and are used for different C# constructs.
A literal has the value that is written in the source code (what you see is what you get). Values like 10 and “This is a dog” are examples of literals.
Separators, such as semicolons (;), commas (,), and periods (.), separate different elements in C# from each other.
Operators act on operands. Operands combine with operators to form expressions.
Instance variables must be initialized when an object is created. This is either done automatically by the runtime, by a declaration initialization, or by an instance constructor.
An object can hold a reference to another object in an instance variable. Such a permanent relationship is called an association.
An object is created by using the new keyword.
In an object-oriented program, classes collaborate to provide the functionality of the program.
Two classes can collaborate by having a relationship.
Common association relationships are aggregations and compositions.
The Unified Modeling Language (UML) is by far the most popular graphical modeling language used to express and illustrate object-oriented program designs.

138 C# PRIMER PLUS
Review Questions
1.What is lexical analysis?
2.What are the atomic parts of a C# program?
3.What is Pascal casing and camel casing? For which parts of the C# program should they be used?
4.What is the main difference between variables and literals?
5.What are operators and operands? How do they relate?
6.Is 50 an expression? Is (50 + x)? Is public?
7.Give examples of typical keywords in C#.
8.Why is pseudocode not well suited for expressing the overall design of an object-ori- ented program?
9.What kind of relationship does a BankCustomer object have with a BankTeller object? How is this expressed in UML?
10.What kind of relationship does a Heart object have with a HumanBody object. How is this expressed in UML?
11.What kind of relationship does a LightBulb have with a Lamp object. How is this expressed in UML?
12.How is an association relationship implemented in C#?
13.How can instance variables be initialized when an object is created?
14.Describe passenger when declared as in the following line:
private Person passenger;
Programming Exercises
Enable the program in Listing 5.1 to exhibit the following functionality by changing its source code:
1.Print “The simulation has commenced” on the command console right after the program is started.
2.Print “The simulation has ended” as the last thing just before the program is terminated.
3.Instead of merely choosing floors between 1 and 30, the Person class chooses floors between 0 and 50.
4.On its first ride, Elevator starts at floor number 0 instead of floor number 1.
5.The Elevator object does 10 journeys instead of the 5 it is doing now.

Chapter 5 • YOUR FIRST OBJECT-ORIENTED C# PROGRAM 139
6. Elevator is currently counting the total floors traveled with the totalFloorsTraveled variable. Declare an instance variable inside the Elevator class that keeps track of the number of trips this elevator completes. You could call this variable totalTripsTraveled. The Elevator should update this variable by adding one to totalTripsTraveled after every trip. Update the ReportStatistics method of the
Elevator class to print out not only totalFloorsTraveled but also totalTripsTraveled, accompanied by an explanation for what is being printed.
7.Add an instance variable to the Elevator class that can hold the name of the elevator. You can call it myName. Which type should you use for this instance variable? Should you use private or public when you declare it? Write a constructor by which you can set the value of this variable as you create the Elevator object with new and assign it to a variable. (Hint: The constructor is a method and must have the same name as its class. This constructor must have a formal parameter of type string in its header.) Adjust the call to the constructor when using the keyword new by inserting the name of the Elevator as an argument (between the parenthesis—instead of new Elevator(), write new Elevator(“ElevatorA”).
Every time the Elevator completes a trip, it should print its name along with its departing and arrival floor. In other words, instead of printing
Departing floor: 2 Traveling to floor: 24
it should print
ElevatorA: Departing floor: 2 Traveling to floor: 24
where ElevatorA is the name of the Elevator residing inside myName.

Introduction to Windows Forms
IN THIS CHAPTER
• |
The Hello Windows Forms Application |
189 |
• |
Creating and Using an Event Handler |
192 |
• |
Defining the Border Style of the Form |
195 |
• |
Adding a Menu 196 |
|
• |
Adding a Menu Shortcut 198 |
|
• |
Handling Events from Menus 199 |
|
CHAPTER
3.1