Bài giảng lập trình c 2010 chương 3 đh công nghệ đồng nai

47 261 0
Bài giảng lập trình c 2010  chương 3   đh công nghệ đồng nai

Đ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

DONG NAI UNIVERSITY OF TECHNOLOGY Format Code Format DONG NAI UNIVERSITY OF TECHNOLOGY Description Code C or c Formats the string as currency Precedes the number with an appropriate currency symbol ($ in the US) D or d Formats the string as a decimal Displays number as an integer N or n Formats the string with commas and two decimal places Format Code Format DONG NAI UNIVERSITY OF TECHNOLOGY Description Code E or e Formats the number using scientific notation with a default of six decimal places F or f Formats the string with a fixed number of decimal places (two by default) G or g General Either E or F X or x Formats the string as hexadecimal DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Logical and Conditional Operators DONG NAI UNIVERSITY OF TECHNOLOGY Logical Conditional AND (&&) AND(&) Logical OR (|) Logical exclusive OR or XOR (^) Conditional OR (||) Logical NOT (!) DONG NAI UNIVERSITY OF TECHNOLOGY Used to add multiple conditions to a statement DONG NAI UNIVERSITY OF TECHNOLOGY exp1 exp2 exp1 && exp2 false false false false true false true false false true true true Truth table for the && (logical AND) operator DONG NAI UNIVERSITY OF TECHNOLOGY Exp1 exp2 exp1 || exp2 false false false false true true true false true true true True Truth table for the || (logical OR) operator DONG NAI UNIVERSITY OF TECHNOLOGY exp1 exp2 exp1 ^ exp2 false false false false true true true false true true true false Truth table for the logical exclusive OR (^) operator DONG NAI UNIVERSITY OF TECHNOLOGY ApplicationException Represents exceptions thrown by the applications SystemException Represents exceptions thrown by the CLR DONG NAI UNIVERSITY OF TECHNOLOGY try Block try { //Code that may cause Exception } DONG NAI UNIVERSITY OF TECHNOLOGY try catch Block {//code that may cause exception } catch (ArithmeticException ex1) {//Process Exception here } catch (EvaluateException ex2) {//Process Exception here } DONG NAI UNIVERSITY OF TECHNOLOGY The throw statement if (n < 0) { ArithmeticException ex1 = new ArithmeticException("n must greater than Zero"); throw ex1; } DONG NAI UNIVERSITY OF TECHNOLOGY finally Block The finally block contains code that always executes, whether or not any exception occurs try {//code here } finally {//do something } Define New Exception class CMyException : ApplicationException { private Exception innerException; private string m_strMsg; public string CustomMessage {get { return this.m_strMsg; } set { this.m_strMsg = value; } } public CMyException(){ } public CMyException(string strMsg,Exception { this.m_strMsg = strMsg; this.innerException = ex; } } ex) DONG NAI UNIVERSITY OF TECHNOLOGY Define New Exception try { int t = 0,n=5; n = n / t; } catch (Exception ex) { CMyException myEx=new CMyException ("Lỗi rồi", ex.InnerException); MessageBox.Show(myEx.CustomMessage); } DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Error provider DONG NAI UNIVERSITY OF TECHNOLOGY Control Name TextBox txtName TextBox txtAge DateTimePicker dateTimePicker1 Button btnDangKy ErrorProvider errorProvider1 DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY private bool validateName(){ bool bValid = true; txtTen.Text.Trim(); if (txtTen.Text == ""){ bValid = false; errorProvider1.SetError(txtTen, "Tên không để trống"); } else{ } return bValid; } errorProvider1.SetError(txtTen, ""); private bool validateAge() { bool bValid = true; int tuoi; bValid = Int32.TryParse(txtTuoi.Text, out tuoi); if (!bValid || tuoi < 18){ errorProvider1.SetError(txtTuoi, "Tuổi nhập không hợp lệ"); bValid = false; }else { errorProvider1.SetError(txtTuoi, ""); } return bValid; } DONG NAI UNIVERSITY OF TECHNOLOGY private bool validateDate() { bool bValid = true; if (dateTimePicker1.Value.DayOfWeek == DayOfWeek.Sunday) { errorProvider1.SetError(dateTimePicker1 , "Không đăng ký vào ngày CN"); bValid = false; } else { errorProvider1.SetError(dateTimePicker1,""); } return bValid; } DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY private void btnDangKy_Click(object sender, EventArgs e) { bool bValidName=validateName(); bool bValidAge=validateAge(); bool bValidDate=validateDate(); if (!bValidName || !bValidAge || !bValidDate) { //Error here } else { //Success here } } DONG NAI UNIVERSITY OF TECHNOLOGY END ... exceptions thrown by the CLR DONG NAI UNIVERSITY OF TECHNOLOGY try Block try { //Code that may cause Exception } DONG NAI UNIVERSITY OF TECHNOLOGY try catch Block {//code that may cause exception... the switch statement DONG NAI UNIVERSITY OF TECHNOLOGY switch Multiple-Selection Structure true case : a case a action(s) break; case b action(s) break; case z action(s) break; false true case :... that may cause exception } catch (ArithmeticException ex1) {//Process Exception here } catch (EvaluateException ex2) {//Process Exception here } DONG NAI UNIVERSITY OF TECHNOLOGY The throw statement

Ngày đăng: 03/12/2015, 18:31

Từ khóa liên quan

Mục lục

  • Slide 1

  • Format Code

  • Format Code

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan