- •Contents at a Glance
- •Table of Contents
- •Acknowledgments
- •Introduction
- •Who This Book Is For
- •Finding Your Best Starting Point in This Book
- •System Requirements
- •Conventions and Features in This Book
- •Conventions
- •Other Features
- •Code Samples
- •Installing the Code Samples
- •Using the Code Samples
- •Support for This Book
- •Questions and Comments
- •Beginning Programming with the Visual Studio 2008 Environment
- •Writing Your First Program
- •Using Namespaces
- •Creating a Graphical Application
- •Chapter 1 Quick Reference
- •Understanding Statements
- •Identifying Keywords
- •Using Variables
- •Naming Variables
- •Declaring Variables
- •Working with Primitive Data Types
- •Displaying Primitive Data Type Values
- •Using Arithmetic Operators
- •Operators and Types
- •Examining Arithmetic Operators
- •Controlling Precedence
- •Using Associativity to Evaluate Expressions
- •Associativity and the Assignment Operator
- •Incrementing and Decrementing Variables
- •Declaring Implicitly Typed Local Variables
- •Chapter 2 Quick Reference
- •Declaring Methods
- •Specifying the Method Declaration Syntax
- •Writing return Statements
- •Calling Methods
- •Specifying the Method Call Syntax
- •Applying Scope
- •Overloading Methods
- •Writing Methods
- •Chapter 3 Quick Reference
- •Declaring Boolean Variables
- •Using Boolean Operators
- •Understanding Equality and Relational Operators
- •Understanding Conditional Logical Operators
- •Summarizing Operator Precedence and Associativity
- •Using if Statements to Make Decisions
- •Understanding if Statement Syntax
- •Using Blocks to Group Statements
- •Cascading if Statements
- •Using switch Statements
- •Understanding switch Statement Syntax
- •Following the switch Statement Rules
- •Chapter 4 Quick Reference
- •Using Compound Assignment Operators
- •Writing while Statements
- •Writing for Statements
- •Understanding for Statement Scope
- •Writing do Statements
- •Chapter 5 Quick Reference
- •Coping with Errors
- •Trying Code and Catching Exceptions
- •Handling an Exception
- •Using Multiple catch Handlers
- •Catching Multiple Exceptions
- •Using Checked and Unchecked Integer Arithmetic
- •Writing Checked Statements
- •Writing Checked Expressions
- •Throwing Exceptions
- •Chapter 6 Quick Reference
- •The Purpose of Encapsulation
- •Controlling Accessibility
- •Working with Constructors
- •Overloading Constructors
- •Understanding static Methods and Data
- •Creating a Shared Field
- •Creating a static Field by Using the const Keyword
- •Chapter 7 Quick Reference
- •Copying Value Type Variables and Classes
- •Understanding Null Values and Nullable Types
- •Using Nullable Types
- •Understanding the Properties of Nullable Types
- •Using ref and out Parameters
- •Creating ref Parameters
- •Creating out Parameters
- •How Computer Memory Is Organized
- •Using the Stack and the Heap
- •The System.Object Class
- •Boxing
- •Unboxing
- •Casting Data Safely
- •The is Operator
- •The as Operator
- •Chapter 8 Quick Reference
- •Declaring an Enumeration
- •Using an Enumeration
- •Choosing Enumeration Literal Values
- •Choosing an Enumeration’s Underlying Type
- •Working with Enumerations
- •Working with Structures
- •Declaring a Structure
- •Understanding Structure and Class Differences
- •Declaring Structure Variables
- •Understanding Structure Initialization
- •Copying Structure Variables
- •Chapter 9 Quick Reference
- •What Is an Array?
- •Declaring Array Variables
- •Creating an Array Instance
- •Initializing Array Variables
- •Creating an Implicitly Typed Array
- •Accessing an Individual Array Element
- •Iterating Through an Array
- •Copying Arrays
- •What Are Collection Classes?
- •The ArrayList Collection Class
- •The Queue Collection Class
- •The Stack Collection Class
- •The Hashtable Collection Class
- •The SortedList Collection Class
- •Using Collection Initializers
- •Comparing Arrays and Collections
- •Using Collection Classes to Play Cards
- •Chapter 10 Quick Reference
- •Using Array Arguments
- •Declaring a params Array
- •Using params object[ ]
- •Using a params Array
- •Chapter 11 Quick Reference
- •What Is Inheritance?
- •Using Inheritance
- •Base Classes and Derived Classes
- •Calling Base Class Constructors
- •Assigning Classes
- •Declaring new Methods
- •Declaring Virtual Methods
- •Declaring override Methods
- •Understanding protected Access
- •Understanding Extension Methods
- •Chapter 12 Quick Reference
- •Understanding Interfaces
- •Interface Syntax
- •Interface Restrictions
- •Implementing an Interface
- •Referencing a Class Through Its Interface
- •Working with Multiple Interfaces
- •Abstract Classes
- •Abstract Methods
- •Sealed Classes
- •Sealed Methods
- •Implementing an Extensible Framework
- •Summarizing Keyword Combinations
- •Chapter 13 Quick Reference
- •The Life and Times of an Object
- •Writing Destructors
- •Why Use the Garbage Collector?
- •How Does the Garbage Collector Work?
- •Recommendations
- •Resource Management
- •Disposal Methods
- •Exception-Safe Disposal
- •The using Statement
- •Calling the Dispose Method from a Destructor
- •Making Code Exception-Safe
- •Chapter 14 Quick Reference
- •Implementing Encapsulation by Using Methods
- •What Are Properties?
- •Using Properties
- •Read-Only Properties
- •Write-Only Properties
- •Property Accessibility
- •Understanding the Property Restrictions
- •Declaring Interface Properties
- •Using Properties in a Windows Application
- •Generating Automatic Properties
- •Initializing Objects by Using Properties
- •Chapter 15 Quick Reference
- •What Is an Indexer?
- •An Example That Doesn’t Use Indexers
- •The Same Example Using Indexers
- •Understanding Indexer Accessors
- •Comparing Indexers and Arrays
- •Indexers in Interfaces
- •Using Indexers in a Windows Application
- •Chapter 16 Quick Reference
- •Declaring and Using Delegates
- •The Automated Factory Scenario
- •Implementing the Factory Without Using Delegates
- •Implementing the Factory by Using a Delegate
- •Using Delegates
- •Lambda Expressions and Delegates
- •Creating a Method Adapter
- •Using a Lambda Expression as an Adapter
- •The Form of Lambda Expressions
- •Declaring an Event
- •Subscribing to an Event
- •Unsubscribing from an Event
- •Raising an Event
- •Understanding WPF User Interface Events
- •Using Events
- •Chapter 17 Quick Reference
- •The Problem with objects
- •The Generics Solution
- •Generics vs. Generalized Classes
- •Generics and Constraints
- •Creating a Generic Class
- •The Theory of Binary Trees
- •Building a Binary Tree Class by Using Generics
- •Creating a Generic Method
- •Chapter 18 Quick Reference
- •Enumerating the Elements in a Collection
- •Manually Implementing an Enumerator
- •Implementing the IEnumerable Interface
- •Implementing an Enumerator by Using an Iterator
- •A Simple Iterator
- •Chapter 19 Quick Reference
- •What Is Language Integrated Query (LINQ)?
- •Using LINQ in a C# Application
- •Selecting Data
- •Filtering Data
- •Ordering, Grouping, and Aggregating Data
- •Joining Data
- •Using Query Operators
- •Querying Data in Tree<TItem> Objects
- •LINQ and Deferred Evaluation
- •Chapter 20 Quick Reference
- •Understanding Operators
- •Operator Constraints
- •Overloaded Operators
- •Creating Symmetric Operators
- •Understanding Compound Assignment
- •Declaring Increment and Decrement Operators
- •Implementing an Operator
- •Understanding Conversion Operators
- •Providing Built-In Conversions
- •Creating Symmetric Operators, Revisited
- •Adding an Implicit Conversion Operator
- •Chapter 21 Quick Reference
- •Creating a WPF Application
- •Creating a Windows Presentation Foundation Application
- •Adding Controls to the Form
- •Using WPF Controls
- •Changing Properties Dynamically
- •Handling Events in a WPF Form
- •Processing Events in Windows Forms
- •Chapter 22 Quick Reference
- •Menu Guidelines and Style
- •Menus and Menu Events
- •Creating a Menu
- •Handling Menu Events
- •Shortcut Menus
- •Creating Shortcut Menus
- •Windows Common Dialog Boxes
- •Using the SaveFileDialog Class
- •Chapter 23 Quick Reference
- •Validating Data
- •Strategies for Validating User Input
- •An Example—Customer Information Maintenance
- •Performing Validation by Using Data Binding
- •Changing the Point at Which Validation Occurs
- •Chapter 24 Quick Reference
- •Querying a Database by Using ADO.NET
- •The Northwind Database
- •Creating the Database
- •Using ADO.NET to Query Order Information
- •Querying a Database by Using DLINQ
- •Creating and Running a DLINQ Query
- •Deferred and Immediate Fetching
- •Joining Tables and Creating Relationships
- •Deferred and Immediate Fetching Revisited
- •Using DLINQ to Query Order Information
- •Chapter 25 Quick Reference
- •Using Data Binding with DLINQ
- •Using DLINQ to Modify Data
- •Updating Existing Data
- •Adding and Deleting Data
- •Chapter 26 Quick Reference
- •Understanding the Internet as an Infrastructure
- •Understanding Web Server Requests and Responses
- •Managing State
- •Understanding ASP.NET
- •Creating Web Applications with ASP.NET
- •Building an ASP.NET Application
- •Understanding Server Controls
- •Creating and Using a Theme
- •Chapter 27 Quick Reference
- •Comparing Server and Client Validations
- •Validating Data at the Web Server
- •Validating Data in the Web Browser
- •Implementing Client Validation
- •Chapter 28 Quick Reference
- •Managing Security
- •Understanding Forms-Based Security
- •Implementing Forms-Based Security
- •Querying and Displaying Data
- •Understanding the Web Forms GridView Control
- •Displaying Customer and Order History Information
- •Paging Data
- •Editing Data
- •Updating Rows Through a GridView Control
- •Navigating Between Forms
- •Chapter 29 Quick Reference
- •What Is a Web Service?
- •The Role of SOAP
- •What Is the Web Services Description Language?
- •Nonfunctional Requirements of Web Services
- •The Role of Windows Communication Foundation
- •Building a Web Service
- •Creating the ProductsService Web Service
- •Web Services, Clients, and Proxies
- •Talking SOAP: The Easy Way
- •Consuming the ProductsService Web Service
- •Chapter 30 Quick Reference
114Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008
4.Click Quit, and return to the Visual Studio 2008 programming environment.
5.In the Code and Text Editor window displaying Window1.xaml.cs, locate the multiplyValues method. It looks like this:
private int multiplyValues(int leftHandSide, int rightHandSide)
{
expression.Text = leftHandSide.ToString() + “ * “ + rightHandSide.ToString(); return leftHandSide * rightHandSide;
}
The return statement contains the multiplication operation that is silently overflowing.
6. Edit the return statement so that the return value is checked, like this:
return checked(leftHandSide * rightHandSide);
The multiplication is now checked and will throw an OverflowException rather than silently returning the wrong answer.
7.Locate the calculateClick method.
8.Add the following catch handler immediately after the existing FormatException catch handler in the calculateClick method:
catch (OverflowException oEx)
{
result.Text = oEx.Message;
}
Tip The logic of this catch handler is the same as that for the FormatException catch
handler. However, it is still worth keeping these handlers separate rather than simply writing a generic Exception catch handler because you might decide to handle these
exceptions differently in the future.
9.On the Debug menu, click Start Without Debugging to build and run the application.
10.Type 9876543 in the left operand text box, type 9876543 in the right operand text box, select the Multiplication option, and then click Calculate.
The second catch handler successfully catches the OverflowException and displays the message “Arithmetic operation resulted in an overflow” in the Result text box.
11.Click Quit to return to the Visual Studio 2008 programming environment.
Throwing Exceptions
Suppose you are implementing a method called monthName that accepts a single int argument and returns the name of the corresponding month. For example, monthName(1) returns “January”, monthName(2) returns “February”, and so on. The question is: What should
Chapter 6 Managing Errors and Exceptions |
115 |
the method return when the integer argument is less than 1 or greater than 12? The best answer is that the method shouldn’t return anything at all; it should throw an exception. The .NET Framework class libraries contain lots of exception classes specifically designed for situations such as this. Most of the time, you will find that one of these classes describes your exceptional condition. (If not, you can easily create your own exception class, but you
need to know a bit more about the C# language before you can do that.) In this case, the existing .NET Framework ArgumentOutOfRangeException class is just right. You can throw an
exception by using the throw statement, as shown in the following example:
public static string monthName(int month)
{
switch (month)
{
case 1 :
return “January”; case 2 :
return “February”;
...
case 12 :
return “December”; default :
throw new ArgumentOutOfRangeException(“Bad month”);
}
}
The throw statement needs an exception object to throw. This object contains the details of
the exception, including any error messages. This example uses an expression that creates a new ArgumentOutOfRangeException object. The object is initialized with a string that will populate its Message property by using a constructor. Constructors are covered in detail in
Chapter 7, “Creating and Managing Classes and Objects.”
In the following exercises, you will add to the MathsOperators project code for throwing an exception.
Throw your own exception
1.Return to Visual Studio 2008.
2.On the Debug menu, click Start Without Debugging.
3.Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate.
The value 0 appears in the Result text box. The fact that you have not selected an
operator option is not immediately obvious. It would be useful to write a diagnostic message in the Result text box.
4.Click Quit to return to the Visual Studio 2008 programming environment.
116Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008
5.In the Code and Text Editor window displaying Window1.xaml.cs, locate and examine the doCalculation method. It looks like this:
private int doCalculation(int leftHandSide, int rightHandSide) { int result = 0;
if (addition.IsChecked.HasValue && addition.IsChecked.Value) result = addValues(leftHandSide, rightHandSide);
else if (subtraction.IsChecked.HasValue && subtraction.IsChecked.Value) result = subtractValues(leftHandSide, rightHandSide);
else if (multiplication.IsChecked.HasValue && multiplication.IsChecked.Value) result = multiplyValues(leftHandSide, rightHandSide);
else if (division.IsChecked.HasValue && division.IsChecked.Value) result = divideValues(leftHandSide, rightHandSide);
else if (remainder.IsChecked.HasValue && remainder.IsChecked.Value) result = remainderValues(leftHandSide, rightHandSide);
return result;
}
The addition, subtraction, multiplication, division, and remainder fields are the radio buttons that appear on the form. Each radio button has a property called IsChecked that indicates whether the user has selected it. The IsChecked property is an example of a nullable value, which means it can either contain a specific value or be in an unde-
fined state. (You learn more about nullable values in Chapter 8, “Understanding Values and References.”) The IsChecked.HasValue property indicates whether the radio button is in a defined state, and if it is, the IsChecked.Value property indicates what this state is. The IsChecked.Value property is a Boolean that has the value true if the radio button
is selected or false otherwise. The cascading if statement examines each radio button in turn to find which one is selected. (The radio buttons are mutually exclusive, so the
user can select only one radio button at most.) If none of the buttons is selected, none of the if statements will be true and the result variable will remain at its initial value (0).
This variable holds the value that is returned by the method.
You could try to solve the problem by adding one more else statement to the if-else cascade to write a message to the result text box on the form. However, this solution is
not a good idea because it is not really the purpose of this method to output messages. It is better to separate the detection and signaling of an error from the catching and handling of that error.
6.Add another else statement to the list of if-else statements (immediately before the return statement), and throw an InvalidOperationException exactly as follows:
else
throw new InvalidOperationException(“no operator selected”);
7.On the Debug menu, click Start Without Debugging to build and run the application.
8.Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate.
Chapter 6 Managing Errors and Exceptions |
117 |
An exception dialog box appears. The application has thrown an exception, but your code does not catch it yet.
9.Click Close program.
The application terminates, and you return to Visual Studio 2008.
Now that you have written a throw statement and verified that it throws an exception, you will write a catch handler to handle this exception.
Catch your own exception
1.In the Code and Text Editor window displaying Window1.xaml.cs, locate the calculateClick method.
2.Add the following catch handler immediately below the existing two catch handlers in the calculateClick method:
catch (InvalidOperationException ioEx)
{
result.Text = ioEx.Message;
}
This code catches the InvalidOperationException that is thrown when no operator radio button is selected.
3.On the Debug menu, click Start Without Debugging.
4.Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate.
The message “no operator selected” appears in the Result text box.
5.Click Quit.
The application is now a lot more robust than it was. However, several exceptions could still
arise that would not be caught and that might cause the application to fail. For example, if you attempt to divide by 0, an unhandled DivideByZeroException will be thrown. (Integer divi-
sion by 0 does throw an exception, unlike floating-point division by 0.) One way to solve this is to write an ever larger number of catch handlers inside the calculateClick method. However, a better solution is to add a general catch handler that catches Exception at the end of the list of catch handlers. This will trap all unhandled exceptions.
Tip The decision of whether to catch all unhandled exceptions explicitly in a method depends on the nature of the application you are building. In some cases, it makes sense to catch exceptions as close as possible to the point at which they occur. In other situations, it is more useful to let an exception propagate back to the method that invoked the routine that threw the exception.
118 |
Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 |
Catch unhandled exceptions
1.In the Code and Text Editor window displaying Window1.xaml.cs, locate the calculateClick method.
2.Add the following catch handler to the end of the list of existing catch handlers:
catch (Exception ex)
{
result.Text = ex.Message;
}
This catch handler will catch all hitherto unhandled exceptions, whatever their specific type.
3.On the Debug menu, click Start Without Debugging.
You will now attempt to perform some calculations known to cause exceptions and confirm that they are all handled correctly.
4.Type 24 in the left operand text box, type 36 in the right operand text box, and then click Calculate.
Confirm that the diagnostic message “no operator selected” still appears in the Result text box. This message was generated by the InvalidOperationException handler.
5.Type John in the left operand text box, and then click Calculate.
Confirm that the diagnostic message “Input string was not in a correct format” appears in the Result text box. This message was generated by the FormatException handler.
6.Type 24 in the left operand text box, type 0 in the right operand text box, select the Divide radio button, and then click Calculate.
Confirm that the diagnostic message “Attempted to divide by zero” appears in the Result text box. This message was generated by the general Exception handler.
7.Click Quit.
Using a finally Block
It is important to remember that when an exception is thrown, it changes the flow of execution through the program. This means you can’t guarantee that a statement will always
run when the previous statement finishes because the previous statement might throw an exception. Look at the following example. It’s very easy to assume that the call to reader.Close
will always occur. After all, it’s right there in the code:
TextReader reader = src.OpenText(); string line;
while ((line = reader.ReadLine()) != null)
{
Chapter 6 Managing Errors and Exceptions |
119 |
source.Text += line + “\n”;
}
reader.Close();
Sometimes it’s not an issue if one particular statement does not run, but on many occasions it can be a big problem. If the statement releases a resource that was acquired in a previous
statement, failing to execute this statement results in the resource being retained. This example is just such a case: If the call to src.OpenText succeeds, it acquires a resource (a file handle) and you must ensure that you call reader.Close to release the resource. If you don’t, sooner or
later you’ll run out of file handles and be unable to open more files. (If you find file handles too trivial, think of database connections instead.)
The way to ensure that a statement is always run, whether or not an exception has been
thrown, is to write that statement inside a finally block. A finally block occurs immediately after a try block or immediately after the last catch handler after a try block. As long as the program enters the try block associated with a finally block, the finally block will always be
run, even if an exception occurs. If an exception is thrown and caught locally, the exception handler executes first, followed by the finally block. If the exception is not caught locally (the
runtime has to search through the list of calling methods to find a handler), the finally block runs first. In any case, the finally block always executes.
The solution to the reader.Close problem is as follows:
TextReader reader = null; try
{
reader = src.OpenText(); string line;
while ((line = reader.ReadLine()) != null)
{
source.Text += line + “\n”;
}
}
finally
{
if (reader != null)
{
reader.Close();
}
}
Even if an exception is thrown, the finally block ensures that the reader.Close statement always executes. You’ll see another way to solve this problem in Chapter 14, “Using Garbage Collection and Resource Management.”
If you want to continue to the next chapter
Keep Visual Studio 2008 running, and turn to Chapter 7.
