CHAPTER THREE Representing Data: Constants and Variables

advertisement
CHAPTER THREE
Representing Data: Constants
and Variables
3- 2
Chapter Introduction
• 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.
3- 3
Objectives
• 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- 4
3.1 Data Categorization
• 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- 5
3.2 Constants
• 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- 6
3.2 Constants (cont.)
• 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.
3- 7
Literal Constants
• 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.
3- 8
Literal Constants (cont.)
• 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.
3- 9
Literal Constants (cont.)
• 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.
3- 10
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.
3- 11
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.
3- 12
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.
3- 13
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.
3- 14
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.
Choosing the Best Data Type
for a Variable
3- 15
• 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.
Declaring Variables:
The Dim Statement
3- 16
• 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.
Using Variables:
The Assignment Statement
3- 17
• 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.
Using Variables:
The Assignment Statement (cont.)
3- 18
• 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.
Using Variables:
The Assignment Statement (cont.)
3- 19
• 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.
Using Variables:
The Assignment Statement (cont.)
3- 20
• 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.
Using Variables:
The Assignment Statement (cont.)
3- 21
• 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.
3- 22
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.
3- 23
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.
3- 24
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.
3- 25
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.
3- 26
Events
• Most common for a form are the Activate
and Load events.
• Only one form can be activate at any given
time.
• The user interacts with the active form.
• An Activate event occurs when the user
switches forms.
• A Load event occurs each time a form is
loaded.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 27
Forms and Main Memory
• In large projects the developer must
manage forms to conserve RAM at run
time.
• Forms must be Loaded and Unloaded.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 28
Run Time Speed
• Transferring data or code between storage
and RAM slows down processing speed.
• Loading and unloading of forms should be
minimized.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 29
Forms and Disk Storage
• Forms are the main way VB organizes the
storage of a project on disk.
• One file is saved for each form.
• A form file contains information about the
form.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 30
Forms and Code Windows
• Each form has its own Code window.
• Code window will show Name property of
related form.
• Code window will show only the event
procedures on the related form.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 31
Form as a Class
• You create a class template by creating a
form.
• Buttons and text boxes are added to the
class definition.
• Events are methods added to the form
class.
• Many properties and methods are
inherited from the form’s superclass.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 32
3.6 Variable Scope
• The domain within which a variable can be
accessed.
• Set of all the code that can refer to a
variable.
• Determined by where and how the
variable is declared.
• There are four levels: block, procedure
(local), module, and global.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 33
Block- and Procedure-Level Scope
• Any variable declared inside a procedure
has procedure-level scope.
• Variable can only be accessed by
statements in that procedure.
• Scope can be narrowed to a single block
of code within the procedure.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 34
Module-Level Variables
• The scope is the set of all procedures
associated with the form.
• Any statement in any procedure belonging
to the form can access a module-level
variable.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 35
Global Variables
• Variables that can be shared across all
forms have global scope.
– The Public Statement
• Used to create a global variable.
– Modules
• Repository for data that need to be shared by
forms.
• Repository for global variables.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 36
Global Variables (cont.)
– Hiding Global Variables
• A procedure-level variable in a procedure “hides” a
global variable with the same name.
• A module-level variable will “hides” a global
variable with the same name.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 37
Global Variables (cont.)
– Procedure-Level, Module-Level, and Global
Scope
• A procedure-level variable is declared in a
procedure using a Dim statement.
• A module-level variable is declared in the
declarations section of a form using a Dim
statement.
• A global variable is declared in the declarations
section of a module using the Public statement.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 38
Global Variables (cont.)
• Project Structure
– Project
• Forms
– General Declarations Section
– Controls
» Properties
» Event Procedures
» Methods
• Code Modules
– General Declarations Section
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 39
3.7 Variable Lifetime
• Period of time a variable exists.
– Static Variables
• Maintain the lifetime of a procedure-level variable
beyond the termination of its procedure.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 40
3.8 Constant Scope
• Symbolic constants have the same levels
of scope as variables.
– Use the Public Const statement to create a
global constant.
• Ex. Public Const DAYSINWEEK = 7
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 41
Chapter Summary
• The two basic kinds of data items are numeric
and string.
• Data items are constants or variables.
• Constants cannot change during program
execution.
• Constants are literal or symbolic.
• Variables are symbolic names for memory
locations.
• Variables can or do change during program
execution.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 42
Chapter Summary (cont.)
• Variables must be declared.
• Expressions are combinations of variables,
constants, and operators that produce a value.
• An assignment statement is used to store values
into a variable or control property.
• A project can have multiple forms, but Visual
Basic .NET allows only one active form at a
time.
• The Show method activates a form.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 43
Chapter Summary (cont.)
• The variable’s scope is the domain within
which the variable can be accessed.
• The three levels of scope are procedurelevel, module-level, and global.
• Variable lifetime refers to how long a
variable exists.
McGraw-Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
Download