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

Chapter 3 Variables, Constants and Calculations

26 275 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 26
Dung lượng 168,78 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

© 20093- 7 Declaration declared before being used in code C# – Reserves an area of memory – Assigns it a name called an Identifier – Required Value for Constants – Optional Initial Value

Trang 1

changed during project execution

• Named Constant

changed during project execution

Trang 3

© 2009

3- 7

Declaration

declared before being used in code

C#

– Reserves an area of memory

– Assigns it a name called an Identifier

– Required Value for Constants

– Optional Initial Value for Variables

decimal decDT, decCD, decCR;

decimal decHour, decSumSal, decDiemTB, decSum=0;

decimal decTAX = 0.12, decHSLUONG=3.16;

Const decimal decDISCOUNT_RATE = 0.15M;

Declaration Examples

Trang 4

© 2009

3- 10

Variables – Scope & Lifetime

– Available to all modules and procedures of Project

– Initialized at start of Project

– Available to one module and all procedures within that module

– Initialized 1 st time the Form is loaded

– Available only to the procedure it is declared in

– Initialized every time the Procedure runs

• Block (not used until later in this course)

– Available only to the block of code inside a procedure it is declared in

– Initialized every time the Procedure runs

– Names can include letters, digits, and the underscore, but must

begin with a letter

– Names cannot contain spaces or periods

Names cannot be # reserved words such as new, class, public, or

conts (keyworks)

– Names are not case sensitive

– For all intensive purposes, a Name can be as long as you want (the

actual limit is 16,383 characters in length).

Accessibility Domains

Accessible from anywhere inside this class

or in any class that inherits from this class.

Protected

Accessible from anywhere inside this class.

Private

Accessible from anywhere in the program

or from any other program that references this one.

Public

Description Keyword

Trang 5

© 2009

3- 13

Scope (Phạm vi biến)

Public

– Available to all modules and procedures of Project

Must be referenced by the namespace

– Available only to the procedure it is declared in

– Initialized every time the Procedure runs

Block

– Available only to the block of code inside a procedure it is declared in

– Initialized every time the Procedure runs

const decimal m decSUADA = 2.5D;

decimal m decSubToTal1KH, decToTalDue1KHCoThue;

int m intTongSoKH;

Trang 6

© 2009

3- 16

Declaring Module Level Variables Example

Module - Level Variables and Constants

Block Level Variables

Block Level Variables

Calculations

• Calculations can be performed using

properties of certain objects, variables,

constants, and numeric literals

• Do Not use Strings in calculations

• Values from Text property of Text Boxes

Trang 7

© 2009

3- 19

Conversion Examples

Function Name

Argument

To Be Acted Upon

intQuantity = Convert.ToInt32 (txtQuantity.Text)

decPrice = Convert.ToDecimal (txtPrice.Text)

intWholeNumber = Convert.ToInt32 (decFractionalValue)

decDollars = Convert.ToDecimal (intDollars)

strValue = Convert.ToString (decValue)

© 2009

3- 20

Mathematical Operators

Modulus (division's remainder)

%

Division/

Multiplication

*

Subtraction-

Addition

+

Operation Operator

Mathematical Order of

Operations

• Computers solve math formulas based on a

specific order 1st, then left to right

Trang 8

© 2009

3- 22

Mathematical Examples

• Note the use of parentheses to control

3+4*2 = 11 Multiply then add

(3+4)*2 = 14 Parentheses control: add then multiply

8/4*2 = 4 Same level, left to right: divide then multiply

Trang 9

© 2009

3- 25

Handling Exceptions

• Exceptions occur when user enters

unexpected/invalid data and program code

does not anticipate this possibility, such as

code attempts to run a Numeric Conversion

• Used to catch and handle exceptions;

referred to as error trapping or handling

• Enclose statements that might cause an

error within Try/Catch Block

Trang 10

© 2009

3- 28

Try Block - Example 1

Catches All Exceptions

lblMessage.Text="Error in input data.“

// thông thường báo qua MessageBox.Show (???)

}

© 2009

3- 29

Try Block - Example 2

Catches Specific Exception

Conversion exception, usually caused

by nonnumeric or blank data

Try Block - Example 3

Catches Multiple Specific Exceptions

Trang 11

© 2009

3- 31

MessageBox Object

• Use Show Method of MessageBox to

display special type of window

• Arguments of Show method

• The MessageBox is an Overloaded Method

Overloading – ability to call different versions of a procedure

based on the number and data types of the arguments passed to that

procedure

– The number and data types of the arguments expected by a

procedure are called Signatures

– There are multiple Signatures to choose from

– Arguments must be included to exactly match one of the

Trang 12

Counting & Accumulating Sums

Local/Event level variables reset to 0 each time

the procedure is called

private void btnCong_Click(object sender, EventArgs e)

{ int intSoA, intSoB;

Double dblKetQua;

intSoA = int.Parse (txtA.Text);

intSoB = Convert.ToInt32 (txtB.Text);

dblKetQua = intSoA + intSoB;

lblKQ.Text = dblKetQua.ToString();

Trang 13

© 2009

3- 37

Ví Dụ Dùng Biến Cục Bộ & Try Catch

private void btnCong_Click(object sender, EventArgs e)

Trang 14

changed during project execution

• Named Constant

changed during project execution

Trang 16

© 2009

3- 7

Declaration

declared before being used in code

C#

– Reserves an area of memory

– Assigns it a name called an Identifier

– Required Value for Constants

– Optional Initial Value for Variables

decimal decDT, decCD, decCR;

decimal decHour, decSumSal, decDiemTB, decSum=0;

decimal decTAX = 0.12, decHSLUONG=3.16;

Const decimal decDISCOUNT_RATE = 0.15M;

Note: Constants are named using all

Declaration Examples

Trang 17

© 2009

3- 10

Variables – Scope & Lifetime

– Available to all modules and procedures of Project

– Initialized at start of Project

– Available to one module and all procedures within that module

– Initialized 1 st time the Form is loaded

– Available only to the procedure it is declared in

– Initialized every time the Procedure runs

• Block (not used until later in this course)

– Available only to the block of code inside a procedure it is declared in

– Initialized every time the Procedure runs

– Names can include letters, digits, and the underscore, but must

begin with a letter

– Names cannot contain spaces or periods

Names cannot be # reserved words such as new, class, public, or

conts (keyworks)

– Names are not case sensitive

– For all intensive purposes, a Name can be as long as you want (the

actual limit is 16,383 characters in length).

Accessibility Domains

Accessible from anywhere inside this class

or in any class that inherits from this class.

Protected

Accessible from anywhere inside this class.

Private

Accessible from anywhere in the program

or from any other program that references this one.

Public

Description Keyword

Trang 18

© 2009

3- 13

Scope (Phạm vi biến)

Public

– Available to all modules and procedures of Project

Must be referenced by the namespace

– Available only to the procedure it is declared in

– Initialized every time the Procedure runs

Block

– Available only to the block of code inside a procedure it is declared in

– Initialized every time the Procedure runs

const decimal m decSUADA = 2.5D;

decimal m decSubToTal1KH, decToTalDue1KHCoThue;

int m intTongSoKH;

Trang 19

© 2009

3- 16

Declaring Module Level Variables Example

Module - Level Variables and Constants

Block Level Variables

Block Level Variables

Calculations

• Calculations can be performed using

properties of certain objects, variables,

constants, and numeric literals

• Do Not use Strings in calculations

• Values from Text property of Text Boxes

Trang 20

© 2009

3- 19

Conversion Examples

Function Name

Argument

To Be Acted Upon

intQuantity = Convert.ToInt32 (txtQuantity.Text)

decPrice = Convert.ToDecimal (txtPrice.Text)

intWholeNumber = Convert.ToInt32 (decFractionalValue)

decDollars = Convert.ToDecimal (intDollars)

strValue = Convert.ToString (decValue)

© 2009

3- 20

Mathematical Operators

Modulus (division's remainder)

%

Division/

Multiplication

*

Subtraction-

Addition

+

Operation Operator

Mathematical Order of

Operations

• Computers solve math formulas based on a

specific order 1st, then left to right

Trang 21

© 2009

3- 22

Mathematical Examples

• Note the use of parentheses to control

3+4*2 = 11 Multiply then add

(3+4)*2 = 14 Parentheses control: add then multiply

8/4*2 = 4 Same level, left to right: divide then multiply

Trang 22

© 2009

3- 25

Handling Exceptions

• Exceptions occur when user enters

unexpected/invalid data and program code

does not anticipate this possibility, such as

code attempts to run a Numeric Conversion

• Used to catch and handle exceptions;

referred to as error trapping or handling

• Enclose statements that might cause an

error within Try/Catch Block

Trang 23

© 2009

3- 28

Try Block - Example 1

Catches All Exceptions

lblMessage.Text="Error in input data.“

// thông thường báo qua MessageBox.Show (???)

}

© 2009

3- 29

Try Block - Example 2

Catches Specific Exception

Conversion exception, usually caused

by nonnumeric or blank data

Try Block - Example 3

Catches Multiple Specific Exceptions

Trang 24

© 2009

3- 31

MessageBox Object

• Use Show Method of MessageBox to

display special type of window

• Arguments of Show method

• The MessageBox is an Overloaded Method

Overloading – ability to call different versions of a procedure

based on the number and data types of the arguments passed to that

procedure

– The number and data types of the arguments expected by a

procedure are called Signatures

– There are multiple Signatures to choose from

– Arguments must be included to exactly match one of the

Trang 25

Counting & Accumulating Sums

Local/Event level variables reset to 0 each time

the procedure is called

private void btnCong_Click(object sender, EventArgs e)

{ int intSoA, intSoB;

Double dblKetQua;

intSoA = int.Parse (txtA.Text);

intSoB = Convert.ToInt32 (txtB.Text);

dblKetQua = intSoA + intSoB;

Trang 26

© 2009

3- 37

Ví Dụ Dùng Biến Cục Bộ & Try Catch

private void btnCong_Click(object sender, EventArgs e)

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