Tải bản đầy đủ (.ppt) (43 trang)

Lecture Learning programming using Visual Basic Net – Chapter 3 Representing data Constants and variables

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (89.03 KB, 43 trang )

CHAPTER THREE
Representing Data: Constants
and Variables


Chapter Introduction

3- 2

• Compose event procedures that perform more
sophisticated tasks.
• Focus specifically on data items.
• Continue work with Visual Basic .NET project
structure.
• Determine the meaning of the term scope.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


Objectives

3- 3

• Differentiate between numeric and string data.
• Determine whether a data item should be a
constant or variable.
• Code constants and variables in event
procedures.


• Describe the characteristics and uses of
standard data types.
• Create projects that consist of several forms.
• Explain scope and describe the domain of
variables in procedures and forms.
McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


3.1 Data Categorization

3- 4

• Two broad categories of data are numeric and
string.
– Numeric data must contain only numbers.
– String data can contain any symbol.
– Numeric data is used in arithmetic
calculations.
– String data cannot be used in calculations.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


3.2 Constants


3- 5

• Data item whose value is assigned at design
time and remains the same at run time.
• A literal constant is just a value.
• A symbolic constant is a descriptive name
substituted for a literal constant.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


3.2 Constants (cont.)

3- 6

• Four different kinds of constants:
– Numeric literal.
– String literal.
– Numeric symbolic.
– String symbolic.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.



Literal Constants

3- 7

• Writing Numeric Literal Constants
– Ex. 3.45 +231 .1 9.4E+7

• Writing String Literal Constants
– Ex. “Hello Jean”

• Symbolic Constants
– INTERESTRATE represents 0.045.

• Creating/Choosing Symbolic Constant Names
– Names are chosen by the developer.
– Naming rules must be adhered to.
McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


Literal Constants (cont.)

3- 8

• The Constant Definition Statement
– Ex. Const INTERESTRATE = 0.045
• Run Time: How the Computer Uses Symbolic

Constants
– Stored in a reference table for later use.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


Literal Constants (cont.)

3- 9

• Advantages of Using Symbolic Constants
– Make program easier to understand.
– Reduce the chance of program
inconsistencies.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


Literal Constants (cont.)
• Literal versus Symbolic Constants
– The null string (“”) and numeric data used in
formulas should be the only literal constants.
• Typical Uses of Symbolic Constants
– Prime interest rate.

– Overtime rate.
– Number of print lines for a printed page.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

310


Literal Constants (cont.)
• Predefined Symbolic Constants
– Visual Basic .NET contains a large set.
– Contained in classes, such as the Math and
Color classes.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

311


3.3 Variables







FleetSize
WageRate
AverageAge
MaximumCapacity
NumberOfSeminar
Participants
• EmployeeName

McGraw-Hill/Irwin








NumBidUnits
YtdEarnings
EmployeeNumber
ExtendedPrice
Depreciation
X

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

312



Standard Data Types
• Number of Bytes
– Main memory occupied by a variable.
• Range
– Largest and smallest values that can be
stored in a numeric variable of a given type.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

313


Standard Data Types (cont.)
• Precision
– Indicates how close together two numeric
values can be.
• Speed of Arithmetic Calculation
– Differs for the different data types.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

314



Choosing the Best Data Type
for a Variable
• Use decision rules
– Ex. Boolean is the best type for a variable that
may be true or false.
– Ex. Decimal is the best type for a dollar
amount.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

315


Declaring Variables:
The Dim Statement
• A variable declaration statement.
– Examples:






Dim StreetAddress As String
Dim GrossWeight As Integer

Dim HomePhone As String
Dim NetIncome As Decimal
Dim CurrentStudent As Boolean

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

316


Using Variables:
The Assignment Statement
• Syntax of the Assignment Statement
– variablename = expression
• Ex. CourseCode = “CISB119”

• Run Time: The Effect of the Assignment
Statement
– Evaluates expression on right side of equal
sign.
– Stores value of expression in variable on left
side of equal sign.
McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

317



Using Variables:
The Assignment Statement (cont.)
• Run Time: How the Computer Evaluates
Expressions
– Computer determines the identity of each
component of the expression.
– Computer performs the indicated operations
to calculate a value.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

318


Using Variables:
The Assignment Statement (cont.)
• Changing Variable Values during Execution
– Storing a value in a variable will overwrite any
existing value.
• Assignment Statements with Strings
– To store the result of string manipulations in
string variables.

McGraw-Hill/Irwin


©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

319


Using Variables:
The Assignment Statement (cont.)
• The Type Mismatch Error
– Trying to store string data in a numeric
variable.
• The Try/Catch Block
– Used to detect and handle errors that are
encountered during run time.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

320


Using Variables:
The Assignment Statement (cont.)
• Control Properties in Assignment Statements
– Ex. lblHomePrice.Text = 210000
• Why Use Variables
– Ideal to store results of intermediate
calculations.

– Values stored in variables may be retrieved
and formatted.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

321


Option Explicit
• Removes the requirement to declare all
variables.
• Highly recommended that this option is ON.
• Helpful in reducing typographical errors.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

322


3.4 The Windows Form Control
• Background of our user interface.
• Organizes a project.

McGraw-Hill/Irwin


©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

323


Appearance and Use
• Multiple forms may be used for large projects.
– Each form should represent an objective.
– Each form should be clear and attractive.
• Each form is a user interface window during run
time.
• All forms have the same basic components.

McGraw-Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

324


Properties of the Form Control







(Name)
AcceptButton
BackColor
CancelButton
ControlBox

McGraw-Hill/Irwin






Font
MaximizeBox
MinimizeBox
Text

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

325


×