1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 3 Error Handling for the User Interface

36 1,1K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 36
Dung lượng 280,5 KB

Nội dung

Chapter 3 : Error Handling for the User Interface Slide 2 9:06:37 AM UNDERSTANDING EXCEPTIONS • An exception occurs when a program encounters any unexpected problems. • Your program should be able to handle these exceptional situations and, if possible, gracefully recover from them. This is called exception handling. Slide 3 9:06:37 AM STEP BY STEP 3_1 Slide 4 9:06:37 AM STEP BY STEP 3_1 Slide 5 9:06:37 AM UNDERSTANDING EXCEPTIONS • The FCL provides two categories of exceptions – ApplicationException Represents exceptions thrown by the applications – SystemException Represents exceptions thrown by the CLR Slide 6 9:06:37 AM HANDLING EXCEPTIONS • The try Block Slide 7 9:06:37 AM HANDLING EXCEPTIONS • The catch Block DivideByZeroExceptionArithmeticException OverflowException FormatException Slide 8 9:06:37 AM HANDLING EXCEPTIONS Slide 9 9:06:37 AM HANDLING EXCEPTIONS Slide 10 9:06:37 AM HANDLING EXCEPTIONS • The throw Statement [...]... focus.) The Validating Event • Inside the Validating event, you can write code to do the following: – Programmatically correct any errors or omissions made by the user – Show error messages and alerts to the user so that the user can fix the problem – Use the Focus() method of the control to transfer the focus back to the field – Set the Cancel property of CancelEventArgs to true This cancels the Validating... field when it contains an error • When the user moves the mouse pointer over the icon, an error message pops up as a ToolTip The ErrorProvider Component Using the ErrorProvider Component and Other Validation Techniques The Validating Event and Sticky Form – The CausesValidation property of the btnExit control to false – Declare the following variable outside a method block in the class: private bool... Add the following code to the Click event handler of the Exit button: The Validating Event and Sticky Form • Attach the following event handling code to the Validating events of both the txtMile controls Methods Math Class Methods • The Math class – Allows the user to perform common math calculations – Using methods • ClassName.MethodName( argument1, arument2, … ) – Constants • Math.PI = 3. 1415926 535 …... the Validating event, leaving the focus in the control The CausesValidation Property • The default value of the CausesValidation property for a control is true for all controls • When you want a control to respond, regardless of the validation status of other controls, you should set the CausesValidation property of that control to false The ErrorProvider Component • The ErrorProvider component can... Contains the code of what the method does • Contains the return value if necessary – For uses call elsewhere in program • Pass parameters if needed – All methods must be defined inside of a class Method Definitions public void MethodName ( ) { // Contains the code of what the method does } public ReturnType methodName(Param1, Param2, …) { //Contains the code of what the method does //Contains the return.. .The throw Statement The throw Statement HANDLING EXCEPTIONS • The finally Block – The finally block contains code that always executes, whether or not any exception occurs Ex: page _ _ _ See Demo VALIDATING USER INPUT • Field-Level Validation – 1 Enter (Occurs when a control is entered.) – – – – 2 GotFocus (Occurs when a control receives focus.) 3 Leave (Occurs when focus... (p 219) • Passing by value – Send a method a copy of the object – When returned are always returned by value – Set by value by default • Passing by reference – Send a method the actual reference point • Causes the variable to be changed throughout the program – When returned are always returned by reference – The ref keyword specifies by reference – The out keyword means a called method will initialize... randomObject.Next() • Returns a number from 0 to Int32.MaxValue – Int32.MaxValue = 2,147,4 83, 647 – randomObject.Next ( x ) • Returns a value from 0 up to but not including x – randomObject.Next ( x, y ) • Returns a number between x and up to but not including y Class Random_ Example Random rand = new Random(); int value; value = rand.Next();// phát sinh 1 số trong [0; 2,147,4 83, 647 value = rand.Next( 6 );// phát... Console.ReadLine() ); double number3 = Convert.ToDouble( Console.ReadLine() ); double result = Maximum( number1, number2, number3 ); Console.WriteLine( "Maximum is: " + result ); } Argument Promotion • Implicit Conversion – Object is converted to a needed type implicitly – Only done if complier knows no data will be lost • Explicit Conversion – Object is manually converted – Required if there could be a loss of... …) { //Contains the code of what the method does //Contains the return value } User- defined method Maximum (p 196) public double Maximum( double x, double y, double z ) { double maximumValue = x; if ( y > maximumValue ) maximumValue = y; if ( z > maximumValue ) maximumValue = z; return maximumValue; } // end method Maximum User- defined method Maximum (p 195) public void DetermineMaximum() { Console.WriteLine( . Validation – 1. Enter (Occurs when a control is entered.) – 2. GotFocus (Occurs when a control receives focus.) – 3. Leave (Occurs when focus leaves a control.) – 4. Validating (Occurs when a control is validating.) – 5 EXCEPTIONS • The try Block Slide 7 9:06:37 AM HANDLING EXCEPTIONS • The catch Block DivideByZeroExceptionArithmeticException OverflowException FormatException Slide 8 9:06:37 AM HANDLING EXCEPTIONS Slide. EXCEPTIONS • The FCL provides two categories of exceptions – ApplicationException Represents exceptions thrown by the applications – SystemException Represents exceptions thrown by the CLR Slide

Ngày đăng: 13/05/2014, 11:39