Top Banner
C#: INTRODUCTION FOR DEVELOPERS Neal Stublen [email protected]
80

Neal Stublen [email protected]. Tonight’s Agenda C# data types Control structures Methods and event handlers Exceptions and validation Q&A.

Dec 26, 2015

Download

Documents

Sibyl Horn
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

C#: INTRODUCTION

FOR DEVELOPERS

Neal Stublen

[email protected]

Page 2: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Tonight’s Agenda

C# data types Control structures Methods and event handlers Exceptions and validation Q&A

Page 3: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

C# DATA TYPES

Page 4: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Built-in Types Integer Types

byte, sbyte, short, ushort, int, uint, long, ulong Floating Point Types

float, double, decimal Character Type

char (2 bytes!) Boolean Type

bool (true or false)

Aliases for .NET data types (Byte, Integer, Decimal, etc.)

Page 5: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Declare and Initialize

<type> <variableName>;int index;float interestRate;

<type> <variableName> = <value>;int index = 0;float interestRate = 0.0005;

Page 6: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Constants

const <type> <variableName> = <value>;

const decimal PI = 3.1415926535897932384626;PI = 3.14; // error!!

Page 7: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Naming Conventions

Camel Notation (camel-case)salesTaxCommon for variables

Pascal Notation (title-case)SalesTaxCommon for type names and constants

C or Python stylesales_tax (variables)SALES_TAX (constants)Not common

Page 8: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Arithmetic Operators

Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) Increment (++)

value++ or ++value Decrement (--)

value-- or --value

Page 9: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Prefix and Postfix Operators Prefix operators modify a variable before it is evaluated Postfix operators modify a variable after it is evaluated

y = 1; x = y++;

// y = 2

// x = 1 (y was incremented AFTER evaluation)

y = 1; x = ++y;

// y = 2

// x = 2 (y was incremented BEFORE evaluation)

Page 10: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Assignment Operators

Assignment (=) Addition (+=)

x = x + y → x += y Subtraction (-=) Multiplication (*=) Division (/=) Modulus (%=)

Page 11: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Order of Precedence

Increment, decrementPrefix versions

Negation Multiplication, division, modulus Addition, subtraction

Evaluate from left to right Evaluate innermost parentheses first

Page 12: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

What Should Happen?

int i = (54 – 15) / 13 - 7;

int i = 39 / 13 - 7;

= 3 - 7;

= -4;

Page 13: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

What Should Happen?

int i = a++ * 4 – b;

int i = (a * 4) - b;a = a + 1;

Page 14: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

What Should Happen?

int i = ++a * 4 – b;

a = a + 1;int i = (a * 4) - b;

Page 15: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

INTRODUCING LINQPAD

Page 16: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

"Advanced" Math Operations

The Math class provides a set of methods for common math operations

totalDollars = Math.Round(totalDollars, 2); Rounds to nearest whole number

hundred = Math.Pow(10, 2); ten = Math.Sqrt(100); one = Math.Min(1, 2); two = Math.Max(1, 2);

Page 17: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

String Declarations// Simple declarationstring text;

// Initial value (double quotes)string text = "Hello!";

// Empty stringstring text = "";

// Unknown value (not an empty string)string text = null;

Page 18: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

String Concatenationstring text = "My name is ";text = text + "Neal"text += " Stublen.“

string first = "Neal";string last = "Stublen";string greeting = “Hi, " + first + " " + last;

double grade = 94;string text = "My grade is " + grade + ".";

Page 19: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Special String Characters Escape sequence New line ("\n") Tab ("\t") Return ("\r") Quotation ("\"") Backslash ("\\") filename = "c:\\dev\\projects"; quote = "He said, \"Yes.\""; filename = @"c:\dev\projects"; quote = @"He said, ""Yes.""";

Page 20: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

CONVERTING BETWEEN DATA TYPES

Page 21: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Type Casting

ImplicitLess precise to more precise

byte->short->int->long->double Automatic casting to more precise types

int letter = 'A';int test = 96, hmwrk = 84;double avg = tests * 0.8 + hmwrk * 0.2;

Page 22: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Type Casting

ExplicitMore precise to less precise

int->char, double->float Must be specified to avoid compiler

errors(<type>) <expression>

double total = 4.56;int avg = (int)(total / 10);decimal decValue = (decimal)avg;

Page 23: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

What Should Happen?

int i = 379;double d = 4.3;byte b = 2;

double d2 = i * d / b;

int i2 = i * d / b;

Page 24: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Converting Types To Strings Everything has a ToString() method

<value>.ToString();

int i = 5;string s = i.ToString();// s = "5“

double d = 5.3;string s = d.ToString();// s = "5.3"

Page 25: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Converting Strings To Types Use the data type’s Parse() method

<type>.Parse(<string>);○ decimal m = Decimal.Parse("5.3");

Use the Convert classConvert.ToInt32(<value>);

○ int i = Convert.ToInt32("Convert.ToBool(<value>);Convert.ToString(<value>);...

Page 26: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Formatted Strings

Formatting codes can customize the output of ToString()<value>.ToString(<formatString>);amount.ToString("c"); // $43.16rate.ToString("p1"); // 3.4%count.ToString("n0"); // 2,345

See p. 121 for formatting codes

Page 27: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Formatted Strings

String class provides a static Format method{index:formatCode}String.Format("{0:c}", 43.16);

○ // $43.16String.Format("{0:p1}", 0.034);

○ // 3.4%

Page 28: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Formatted Strings

String.Format("Look: {0:c}, {1:p1}",43.16, 0.034);

// Look: $43.16, 3.4% String.Format(

“{0}, your score is {1}.",name, score);

// John, your score is 34.

Page 29: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Variable Scope

Scope limits access and lifetime Class scope Method scope Block scope No officially "global" scope

Page 30: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Enumeration Types

Enumerations define types with a fixed number of values

enum StoplightColors{    Red,    Yellow,    Green}

Page 31: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Enumeration Type

Numeric values are implied for each enumerated value

enum StoplightColors{    Red = 10,    Yellow,    Green}

Page 32: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Enumeration Type

enum StoplightColors{    Red = 10,    Yellow = 20,    Green = 30}

Page 33: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Enumeration Typeenum StoplightColors{    Red = 10}

string color =

  StoplightColors.Red.ToString();

// color = "Red", not "10"

Page 34: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

"null" Values Identifies an unknown value

string text = null;

int value = null; // error! int? nonValue = null; bool defined = nonValue.HasValue; int value = nonValue.Value;

decimal? price1 = 19.95m; decimal? price2 = null; decimal? total = price1 + price2;

total = null (unknown, which seems intuitive)

Page 35: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

INVOICE TOTAL ENHANCEMENTS

Page 36: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Form Enhancements

Display a formatted subtotal as a currency

Round the discount amount to two decimal places

Keep running invoice totals Reset the totals when the

button is clicked Chapter 4 exercises

Page 37: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

CHAPTER 5

CONTROL STRUCTURES

Page 38: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Common Control Structures Boolean expressions

Evaluate to true or false Conditional statements

Conditional execution Loops

Repeated execution

Page 39: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Boolean Expressions Equality (==)

a == b Inequality (!=)

a != b Greater than (>)

a > b Less than (<)

a < b Greater than or equal (>=)

a >= b Less than (<=)

a <= b

Page 40: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Logical Operators Combine logical operations Conditional-And (&&)

(file != null) && file.IsOpen Conditional-Or (||)

(key == 'q') || (key == 'Q') And (&)

file1.Close() & file2.Close() Or (|)

file1.Close() | file2.Close() Not (!)

!file.Open()

Page 41: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Logical Equivalence

DeMorgan's Theorem

!(a && b) is the equivalent of (!a || !b) !(a || b) is the equivalent of (!a && !b)

Page 42: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

if-else Statementsif (color == SignalColors.Red){    Stop();}else if (color == SignalColors.Yellow){    Evaluate();}else{    Drive();}

Page 43: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

switch Statementsswitch (color){case SignalColors.Red:

    {        Stop();        break;    }case SignalColors.Yellow:

    {        Evaluate();        break;    }default:

    {          Drive();

        break;    }}

Page 44: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

switch Statementsswitch (color){case SignalColors.Red:

    {        Stop();        break;    }case SignalColors.Yellow: // fall through default:

    {          Drive();

        break;    }}

Page 45: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

switch Statementsswitch (color){case SignalColors.Red: // fall throughcase SignalColors.Yellow: // fall through default:

    {          Drive();

        break;    }}

Page 46: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

switch Statements

switch (name){case "John":case "Johnny":case "Jon":

    {          ...

        break;    }}

Page 47: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

ANOTHER INVOICE TOTAL APPLICATION

Page 48: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Form Enhancements

Select a discount tier based on the customer typeChapter 5 exercises

Page 49: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

while Statements

while (!file.Eof){    file.ReadByte();}

char ch;do{    ch = file.ReadChar();} while (ch != 'q');

Page 50: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

for Statements

int factorial = 1;for (int i = 2; i <= value; ++i){    factorial *= i;}

string digits = ""for (char ch = '9'; ch <= '0'; ch-=1){    digits += ch;}

Page 51: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

break and continue Statements

break allows is to jump out of a loop before reaching its normal termination condition

continue allows us to jump to the next iteration of a loop

Page 52: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

break and continue Statements

string text = "";for (int i = 0; i < 10; i++){    if (i % 2 == 0)        continue;    if (i > 8)        break;    text += i.ToString();}

Page 53: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Caution!

int index = 0; while (++index < lastIndex){    TestIndex(index);}

int index = 0; while (index++ < lastIndex){    TestIndex(index);}

Page 54: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

What About This?

for (int i = 0; i < 10; i++){}

for (int i = 0; i < 10; ++i){}

Page 55: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

DEBUGGING LOOPS

Page 56: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Debugging Summary

Stepping through code (over, into, out) Setting breakpoints Conditional breakpoints

Page 57: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

THE FUTURE VALUE FORM

Page 58: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Form Enhancements

Examine the looping calculation in the Future Value exampleChapter 5 exercises

Page 59: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

CHAPTER 6

CLASS METHODS

Page 60: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Class Methodsclass DiscountCalculator{    private decimal CalcDiscPercent(decimal inAmt)    {        return (inAmt > 250.0m) ? 0.10m: 0.0m;    }

    public decimal CalcDiscAmount(decimal inAmt)    {        decimal percent = CalcDiscPercent(inAmt);        return inAmt * percent;    }}

Page 61: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Access Modifierclass DiscountCalculator{    private decimal CalcDiscPercent(decimal inAmt)    {        return (inAmt > 250.0m) ? 0.10m: 0.0m;    }

    public decimal CalcDiscAmount(decimal inAmt)    {        decimal percent = CalcDiscPercent(inAmt);        return inAmt * percent;    }}

Page 62: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Return Typeclass DiscountCalculator{    private decimal CalcDiscPercent(decimal inAmt)    {        return (inAmt > 250.0m) ? 0.10m: 0.0m;    }

    public decimal CalcDiscAmount(decimal inAmt)    {        decimal percent = CalcDiscPercent(inAmt);        return inAmt * percent;    }}

Page 63: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Method Nameclass DiscountCalculator{    private decimal CalcDiscPercent(decimal inAmt)    {        return (inAmt > 250.0m) ? 0.10m: 0.0m;    }

    public decimal CalcDiscAmount(decimal inAmt)    {        decimal percent = CalcDiscPercent(inAmt);        return inAmt * percent;    }}

Page 64: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Method Parametersclass DiscountCalculator{    private decimal CalcDiscPercent(decimal inAmt)    {        return (inAmt > 250.0m) ? 0.10m: 0.0m;    }

    public decimal CalcDiscAmount(decimal inAmt)    {        decimal percent = CalcDiscPercent(inAmt);        return inAmt * percent;    }}

Page 65: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Return Statementsclass DiscountCalculator{    private decimal CalcDiscPercent(decimal inAmt)    {        return (inAmt > 250.0m) ? 0.10m: 0.0m;    }

    public decimal CalcDiscAmount(decimal inAmt)    {        decimal percent = CalcDiscPercent(inAmt);        return inAmt * percent;    }}

Page 66: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

PASSING PARAMETERS

Page 67: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Parameters Summary

Pass zero or more parameters Parameters can be optional Optional parameters are "pre-defined"

using constant values Optional parameters can be passed by

position or name Recommendation: Use

optional parameters

cautiously

Page 68: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Parameters Summary

Parameters are usually passed by value Parameters can be passed by reference Reference parameters can change the

value of the variable that was passed into the method

Page 69: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

EVENTS AND DELEGATES

Page 70: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Event and Delegate Summary

A delegate connects an event to an event handler.

The delegate specifies the handler’s return type and parameters.

Event handlers can be shared with multiple controlsChapter 6, Exercise 1Clear result when any value

changes

Page 71: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

CHAPTER 7

EXCEPTIONS AND VALIDATION

Page 72: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Exceptions

Exception

Format Exception Arithmetic Exception

OverflowException DivideByZeroException

Page 73: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Format Exception

string value = “ABCDEF”;int number = Convert.ToInt32(value);

Page 74: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Overflow Exception

checked{ byte value = 200; value += 200;

int temp = 5000; byte check = (byte)temp;}

Page 75: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

“Catching” an Exception

try{ int dividend = 20; int divisor = 0; int quotient = dividend / divisor; int next = quotient + 1;}catch{}

Page 76: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Responding to Exceptions A simple message box:

MessageBox.Show(message, title); Set control focus:

txtNumber.Focus();

Page 77: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Catching Multiple Exceptions

try {}catch(FormatException e){}catch(OverflowException e){}catch(Exception e){}finally{}

Page 78: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Throwing an Exceptionthrow new Exception(“Really bad error!”);

try{}catch(FormatException e){ throw e;}

Page 79: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

FUTURE VALUE EXCEPTIONS

Page 80: Neal Stublen nstublen@jccc.edu. Tonight’s Agenda  C# data types  Control structures  Methods and event handlers  Exceptions and validation  Q&A.

Form Enhancements

What exceptions might occur in the future value calculation?Chapter 7 exercises