chapter three representing data: constants and variables
TRANSCRIPT
CHAPTER THREE
Representing Data: Constants and Variables
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 2
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- 3
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- 4
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- 5
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- 6
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- 7
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- 8
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- 9
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- 10
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- 11
3.3 Variables
• FleetSize• WageRate• AverageAge• MaximumCapacity• NumberOfSeminar
Participants• EmployeeName
• NumBidUnits• YtdEarnings• EmployeeNumber• ExtendedPrice• Depreciation• X
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 12
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- 13
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.
3- 14Choosing 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.
3- 15Declaring 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.
3- 16Using 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.
3- 17Using 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.– Example 3.1
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 18Using Variables: The Assignment Statement (cont.)
• Changing Variable Values during Execution– Storing a value in a variable will overwrite any existing
value.– A = 5– A = A + 1
• Assignment Statements with Strings– To store the result of string manipulations in string
variables.– Name = “John”
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 19Using Variables: The Assignment Statement (cont.)
• The Type Mismatch Error– E.g. Trying to store string data in a numeric
variable, or to store number in a string variable
• Dim Name As String• Name = 12345
• 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.
3- 20Using 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.
• Example 3.2
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 21
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- 22
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- 23
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- 24
Properties of the Form Control
• (Name)• AcceptButton• BackColor• CancelButton• ControlBox
• Font• MaximizeBox• MinimizeBox• Text
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 25
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- 26
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- 27
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.
• Example 3.4 (procedure scope)
• Scope can be narrowed to a single block of code within the procedure.
• Example 3.3 (block scope)
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 28
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.
• Example 3.5
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 29
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.
• Example 3.7
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 30
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- 31
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- 32
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- 33
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.
– Example 3.8, 3.9, 3.10
McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
3- 34
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- 35
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- 36
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- 37
Chapter Summary (cont.)
• The variable’s scope is the domain within which the variable can be accessed.
• The three levels of scope are procedure-level, module-level, and global.
• Variable lifetime refers to how long a variable exists.