CIS162AD – C#

22
CIS162AD – C# Formatting and Exceptions 03_exceptions.ppt

description

CIS162AD – C#. Formatting and Exceptions 03_exceptions.ppt. Overview of Topics. Hardware Information Processing Cycle Text Boxes - for input and output Numeric input and output Handling Exceptions IPO Charts. Input Devices Keyboard Mouse Scanner Output Devices Console (Display) - PowerPoint PPT Presentation

Transcript of CIS162AD – C#

Page 1: CIS162AD – C#

CIS162AD – C#

Formatting and Exceptions03_exceptions.ppt

Page 2: CIS162AD – C#

CIS162AD 2

Overview of Topics

Hardware Information Processing Cycle Text Boxes - for input and output Numeric input and output Handling Exceptions IPO Charts

Page 3: CIS162AD – C#

CIS162AD 3

A Computer is an electronic device consisting of

hardware.

Input Devices– Keyboard

– Mouse

– Scanner

Output Devices– Console (Display)

– Printer

Storage Devices– Hard drive

– Diskette

– Zip Disk

Internally– Central Processing Unit

(CPU)

– Memory (RAM)

Page 4: CIS162AD – C#

CIS162AD 4

Hardware Model

Input Devices Memory (RAM) Output Devices

CPU

Storage DevicesThe hardware configuration is used to support the Information Processing Cycle.

Page 5: CIS162AD – C#

CIS162AD 5

Information Processing Cycle

InputRaw Data

Process (Application)

OutputInformation

StorageOutput from one process can serve as input to another process.

Page 6: CIS162AD – C#

CIS162AD 6

What do we need to operate the hardware and transform data into information?

Software!

Page 7: CIS162AD – C#

CIS162AD 7

Software: Two Major Categories

Operating System (OS)– Software that allocates and monitors computer

resources including memory allocation, storage, and security.

Application Software– Software used to process raw data into

information.– We will be developing applications using C#.

Page 8: CIS162AD – C#

CIS162AD 8

Input/Output Recall the Information Processing Cycle

Input Process Output We need a user interface to get their Input

and to display processing results as Output. We use variables to store the values entered

by the user and to store the results of the processing.

In C#, textboxes are the most common control object used to facilitate I/O.

Page 9: CIS162AD – C#

CIS162AD 9

TextBoxes Contents of a textbox is always a String. Can get and assigned values to a textbox. Use the property Text.

string strName;

strName = txtName.Text //Input

txtName.Text = strName //Output

Page 10: CIS162AD – C#

CIS162AD 10

Converting with Parse Method Numbers are also entered as Strings through textboxes. The String must be converted to a number before being

assigned to a numeric variable, and before being used in a arithmetic expression.

Each datatype has a Parse method for conversion.int intQty;decimal decPrice;decimal decSubtotal;

intQty = int.Parse(txtQuantity.Text);decPrice = decimal.Parse(txtPrice.Text);

decSubtotal = intQty * decPrice;

Page 11: CIS162AD – C#

CIS162AD 11

ToString Function To display a number in a textbox it is must be

converted from a number to a String. Use built-in function ToString.

decSubtotal = intQty * decPrice;

txtSubtotal.Text = decSubtotal.ToString(“N”);

N stands for Number: decSubtotal.ToString(“N”); C stands for Currency: decSubtotal.ToString(“C”);

Page 12: CIS162AD – C#

CIS162AD 12

Example All Together

int intQty;decimal decPrice;decimal decSubtotal;

intQty = int.Parse(txtQuantity.Text);decPrice = decimal.Parse(txtPrice.Text);

decSubtotal = intQty * decPrice;

txtSubtotal.Text = decSubtotal.ToString(“N”);

Page 13: CIS162AD – C#

CIS162AD 13

Handling Exceptions When numeric data is requested from the user things

can go wrong. The conversion method (Parse) will fail if the user

enters nonnumeric data or leaves the textbox blank. Each of these situations will cause a run-time error. Each of these situations throws an exception before

the run-time error is display. We can catch the exception with a Try/Catch Block. Catching the exception allows us to handle the error

gracefully.

Page 14: CIS162AD – C#

CIS162AD 14

Try / Catch Blocks Enclose any statements that might cause an error in a try/catch block.

try{

intQty = int.Parse(txtQuantity.Text)}catch (Exception exc){

MessageBox.Show(“Data Entry Error.”)}finally{

txtQuantity.Focus( )}

Use catch section to handle the error. Create a local object (exc) of type Exception for processing. Use finally for statements that should be executed whether or not

an exception occurred. finally section is optional.

Page 15: CIS162AD – C#

CIS162AD 15

Exception Classes Exception classes can be used to catch specific errors.

– InvalidCastException – failure of conversion function.– ArithmeticException – a calculation error– OutOfMemoryException – not enough memory– Exception – use to handle any exceptions not coded.– Use Message property to display error message when an

unexpected error is generated. try

intQty = int.Parse(txtQuanityty.Text) catch (InvalidCastException exc)

MessageBox.Show(“Data Entry Error.”)catch (Exception exc)

MessageBox.Show(“Unexpected Error: ” & exc.Message)finally

Page 16: CIS162AD – C#

CIS162AD 16

MessageBox Object

A message can be displayed to users in a message box, which is a special type of window.

MessageBox.Show(text, titlebar, buttons, icon)– Text is the message displayed to the user.

– Titlebar is used to provide a title for the window.

– Buttons is used to specify the buttons that should be displayed (OK, Cancel, Retry, Yes, No, etc.)

– Icon is used to specify what icon to display in front of the message (Error, Hand, Information, None, Warning, etc.)

Text is required, but the other parameters are optional.

Page 17: CIS162AD – C#

CIS162AD 17

Method Overloading

Method Overloading occurs when methods have the same name but different number or type of parameters.

The Show method for MessageBox is defined many different ways.

The same method name can be used because the argument list for each version makes it unique. This is referred to as the method’s signature.

Every method must have a unique signature. This is a feature of object-oriented programming.

Page 18: CIS162AD – C#

CIS162AD 18

IPO Charts

Input, Processing, and Output (IPO) When designing a program, we usually begin

with the end in mind (output). From the output we can determine the inputs

that will be required to complete the processing.

Page 19: CIS162AD – C#

CIS162AD 19

IPO Chart for CS3

Input Processing OutputQuantity

Price

Constants:

Tax Rate

Shipping Rate

Extended Price = Qty * Price

Tax = Extended Price * Tax Rate

Total due = Extended Price + Tax + Shipping Rate

Etc…

Extended Price

Tax

Shipping

Total Due

Etc…

Page 20: CIS162AD – C#

CIS162AD 20

CS3 Sales Calculator Code//Input

intQuantity = int.Parse(txtQuantity.Text);decPrice = decimal.Parse (txtPrice.Text);

//Process - calculate valuesdecExtendedPrice = intQuantity * decPrice;decSalesTax = decExtendedPrice * cdecTAX_RATE;decTotalDue = decExtendedPrice + decSalesTax + cdecSHIPPING_RATE;

//OutputlblExtendedPrice.Text = decExtendedPrice.ToString("C");lblSalesTax.Text = decSalesTax.ToString("C");lblShipping.Text = cdecSHIPPING_RATE.ToString("C");lblTotalDuel.Text = decTotalDue.ToString("C");

Page 21: CIS162AD – C#

CIS162AD 21

Using IPO Charts Consider using IPO Charts when planning the logic

for your assignments. It is much easier to develop a program if you have a

plan. It will save you a lot of time. It is very difficult to design a program on the fly in

front of the keyboard. Even if you find the first couple assignments “easy”,

you should spend some time designing the solution so that you will have experience when we get to the more complicated assignments.

Page 22: CIS162AD – C#

CIS162AD 22

Summary

Numeric Input and Output Exception Handling IPO Charts