1. Trang chủ
  2. » Công Nghệ Thông Tin

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

43 277 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 43
Dung lượng 291,5 KB

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

Nội dung

The following will be discussed in this chapter 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,...

Trang 1

CHAPTER THREE

Representing Data: Constants

and Variables

Trang 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

Trang 3

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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

Trang 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

Trang 5

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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

Trang 7

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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

Trang 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

Trang 9

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

Literal Constants (cont.)

• Advantages of Using Symbolic Constants

– Make program easier to understand

– Reduce the chance of program

inconsistencies

Trang 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

Trang 11

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

11

Literal Constants (cont.)

• Predefined Symbolic Constants

– Visual Basic NET contains a large set

– Contained in classes, such as the Math and

Color classes

Trang 13

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

– Largest and smallest values that can be

stored in a numeric variable of a given type

Trang 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

Trang 15

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

15

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

Trang 16

The Dim Statement

• A variable declaration statement

– Examples:

• Dim StreetAddress As String

• Dim GrossWeight As Integer

• Dim HomePhone As String

• Dim NetIncome As Decimal

Trang 17

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

17

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

Trang 18

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

Trang 19

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

19

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

Trang 20

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

Trang 21

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

21

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

Trang 22

Option Explicit

• Removes the requirement to declare all

variables

• Highly recommended that this option is ON

• Helpful in reducing typographical errors

Trang 23

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

23

3.4 The Windows Form Control

• Background of our user interface

• Organizes a project

Trang 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

Trang 25

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

Trang 26

• 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

Trang 27

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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

Trang 28

Run Time Speed

• Transferring data or code between storage and

RAM slows down processing speed

• Loading and unloading of forms should be

minimized

Trang 29

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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

Trang 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

Trang 31

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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

Trang 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

Trang 33

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

33

Block- and Procedure-Level Scope

• Any variable declared inside a procedure has

Trang 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

Trang 35

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

35

Global Variables

• Variables that can be shared across all forms

have global scope.

– The Public Statement

• Used to create a global variable.

Trang 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.

Trang 37

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

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.

Trang 38

Global Variables (cont.)

Trang 39

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

Trang 41

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

• 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

Trang 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

Trang 43

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved.

43

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

Ngày đăng: 16/05/2017, 14:40

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN