microsoft visual basic 2010: reloaded fourth edition chapter three memory locations and calculations

79
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Upload: gwenda-mccormick

Post on 17-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded Fourth Edition

Chapter ThreeMemory Locations and Calculations

Page 2: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Objectives

After studying this chapter, you should be able to:

• Declare variables and named constants

• Assign data to an existing variable

• Convert data to the appropriate type using the TryParse method and the Convert class methods

• Write arithmetic expressions

• Understand the scope and lifetime of variables and named constants

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2

Page 3: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Objectives (cont'd.)

• Understand the purpose of the Option statements

• Use a TOE chart, pseudocode, and a flowchart to code an application

• Clear the contents of a control’s Text property during run time

• Send the focus to a control during run time

• Explain the difference between syntax errors and logic errors

• Format an application’s numeric output

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3

Page 4: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Internal Memory

• Internal memory: a component inside a computer comprised of memory locations

• Each memory location has a unique numeric address and can hold only one item at a time

• A programmer can reserve memory locations for a program by assigning each location a name, a data type, and an initial value

• Data type: indicates the type of data the memory location will store

• Two types of memory locations that a programmer can declare: variables and constants

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4

Page 5: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Internal Memory (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5

Figure 3-1: Illustration of storage bins

Page 6: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Variables

• Variables: computer memory locations used to temporarily store data while an application is running– Contents can change during run time

• Use a meaningful variable name that reflects the purpose of the variable

• Use camel casing for variable identifiers

• Variable names should conform to naming rules

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6

Page 7: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7

Figure 3-2: How to name a variable

Page 8: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Variables (cont'd.)

• Each variable must be assigned a data type, which determines the memory location’s data type

• Each data type is a class– Integer, Long, or Short data types can store

integers (whole numbers)– Decimal, Double, and Single data types: store

real numbers (numbers with a decimal place)– Char data type: stores one Unicode character– String data type: stores multiple Unicode

characters

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8

Page 9: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Variables (cont'd.)

• Unicode: – Universal coding scheme for characters that assigns

a unique numeric value to each character

• Other data types– Boolean data type: stores a value of True or False

– Date data type: stores date and time information– Object data type: stores any type of data

• Computer must determine the data type at run time, making it more inefficient

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9

Page 10: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10

Figure 3-3: Basic data types in Visual Basic

Page 11: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11

Declaring a Variable in Code

• Declaration statement: used to declare, or create, a variable– Declaration statement includes:

• Scope keyword: Dim, Private, or Static• Name of the variable and data type• Initial value (optional)

• Initialization– Numeric data types: automatically initialized to 0– String data type: automatically initialized to Nothing– Boolean data type: initialized to False

Page 12: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12

Figure 3-4: How to declare a variable

Declaring a Variable in Code (cont’d.)

Page 13: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13

Figure 3-4: How to declare a variable (cont’d.)

Declaring a Variable in Code (cont’d.)

Page 14: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14

Assigning Data to an Existing Variable

• Assignment statement:– Used to assign values to properties of controls– Used to assign values to variables

• Assignment operator (=):– Expression on the right of the = operator is assigned

to the variable on the left of the = operator

• Expression: can contain literal constants, object properties, variables, keywords, or arithmetic operators

Page 15: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Assigning Data to an Existing Variable (cont'd.)

• Literal constant: – An item of data whose value does not change while

the application is running– Can be a numeric or a string literal constant

• A numeric literal without a decimal place is treated as an integer

• A numeric literal with a decimal place is treated as a Double type

• String literals are enclosed in quotation marks

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15

Page 16: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16

Figure 3-5: How to assign a value to a variable

Assigning Data to an Existing Variable (cont'd.)

Page 17: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17

Figure 3-5: How to assign a value to a variable (cont’d.)

Assigning Data to an Existing Variable (cont'd.)

Page 18: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Using the TryParse Method

• Method: a specific portion of a class’s instructions that performs a task for the class

• TryParse method: – Part of every numeric data type’s class– Used to convert a string to that numeric data type

• Argument: a value that is provided to a method

• Basic syntax of TryParse method has two arguments:– String: string value to be converted– Variable: location to store the result

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18

Page 19: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Using the TryParse Method (cont'd.)

• If TryParse conversion is successful, the method stores the value in the variable

• If unsuccessful, a 0 is stored in the numeric variable

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19

Page 20: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20

Figure 3-6: How to use the basic syntax of the TryParse method

Page 21: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21

Figure 3-7: Result of the TryParse method for the Double, Decimal, and Integer data types

Using the TryParse Method (cont'd.)

Page 22: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Using the Convert Class Methods

• Convert class:– Contains methods for converting numeric values to

specific data types

• Commonly used methods of the Convert class include:– ToDouble– ToDecimal– ToInt32– ToString

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22

Page 23: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23

Figure 3-8: How to use the Convert class methods

Using the Convert Class Methods (cont’d.)

Page 24: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24

Including Variables in Arithmetic Expressions

• Arithmetic operators: used to perform calculations

• Precedence number: indicates the order in which an operation in an expression is performed

• If an expression has two operators with the same precedence, they are evaluated from left to right

• Use parentheses to change the order of evaluation

• Integer division operator (\): divides two integers and returns an integer value

• Modulus arithmetic operator (Mod): divides two numbers and returns the remainder

Page 25: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25

Including Variables in Arithmetic Expressions (cont'd.)

Figure 3-9: Most commonly used arithmetic operators

Page 26: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26

Including Variables in Arithmetic Expressions (cont'd.)

Figure 3-10: How to use the integer division and Mod operators

Page 27: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27

Including Variables in Arithmetic Expressions (cont'd.)

Figure 3-11: Expressions containing more than one operator having the same precedence

Page 28: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28

Figure 3-12: How to use variables and arithmetic operators

Page 29: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29

Figure 3-12: How to use variables and arithmetic operators (cont’d.)

Including Variables in Arithmetic Expressions (cont'd.)

Page 30: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Arithmetic Assignment Operators

• Arithmetic assignment operators: abbreviate an assignment statement that contains an arithmetic operator for specific cases

• Statement must be of the form:variableName =

variableName arithmeticOperator value

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30

Page 31: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31

Arithmetic Assignment Operators (cont’d.)

Figure 3-13: How to use the arithmetic assignment operators

Page 32: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32

Figure 3-13: How to use the arithmetic assignment operators (cont’d.)

Page 33: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

The Scope and Lifetime of a Variable

• Scope: indicates where the variable can be used

• Lifetime: indicates how long the variable remains in memory

• Variables can have module scope, procedure scope, or block scope

• A variable’s scope and lifetime are determined by where you declare the variable– Variables declared in the form’s Declarations section

have class scope– Variables declared within a procedure have either

procedure scope or block scopeMicrosoft Visual Basic 2010: Reloaded, Fourth Edition 33

Page 34: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Variables with Procedure Scope

• Procedure-level variable: declared within a procedure– Use the Dim keyword in the declaration

• Procedure scope: only the procedure can use the variable– With procedure-level scope, two procedures can each

use the same variable names

• Comments: – Used to internally document the procedure– Are ignored by the compiler– Appear in green in the Code Editor

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34

Page 35: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35

Figure 3-14: The MainForm in the Sales Tax application

Figure 3-15: Examples of using procedure-level variables

Page 36: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Variables with Procedure Scope (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36

Figure 3-15: Examples of using procedure-level variables (cont’d.)

Page 37: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Variables with Class Scope

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37

• Class scope: variable can be used by all procedures in the form

• Class-level variable: – Declared in the form’s Declarations section– Use Private keyword in declaration

• Class-level variables retain their values until the application ends

Page 38: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38

Figure 3-17: Example of using a class-level variable

Page 39: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39

Static Variables

• Static variable: – Procedure-level variable that remains in memory and

retains its value even after the procedure ends– Retains its value until the application ends (like a

class-level variable), but can only be used by the procedure in which it is declared

• A static variable has:– Same lifetime as a class-level variable– Narrower scope than a class-level variable

• Declared using the Static keyword

Page 40: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40

Figure 3-18: Example of using a static variable

Static Variables (cont’d.)

Page 41: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Named Constants

• Named constant: memory location whose value cannot be changed while the application is running– Declared using the Const keyword– Good programming practice to specify the data type

as well– Many programmers use Pascal case for named

constants

• Literal type character: forces a literal constant to assume a specific data type

• Named constants help to document the program code

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41

Page 42: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42

Figure 3-19: How to declare a named constant

Page 43: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43

Figure 3-20: Area Calculator application’s interface

Figure 3-21: Example of using a named constant

Page 44: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Option Explicit, Option Infer, and Option Strict

• Undeclared variable: a variable that does not appear in a declaration statement (such as Dim)– Is assigned a data type of Object

• Misspelling a variable name can result in an undeclared variable unless Option Explicit is on

• Option Explicit On statement– Appears in the General Declarations section of the

Code Editor window (above Public Class statement)

– Enforces that all variables must be declared before being used

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44

Page 45: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Option Explicit, Option Infer, and Option Strict (cont'd.)

• Option Infer Off statement: ensures that every variable is declared with a data type

• Implicit type conversion: occurs when you attempt to assign data of one type to a variable of another type without explicitly attempting to convert it– If converted to a data type that can store larger

numbers, the value is said to be promoted– If converted to a data type that can store only

smaller numbers, the value is said to be demoted• Can cause truncation and loss of precision

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45

Page 46: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 46

Figure 3-22: Rules and examples of type conversions

Page 47: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Option Explicit, Option Infer, and Option Strict (cont'd.)

• Option Strict On statement: ensures that values cannot be converted from one data type to a narrower data type, resulting in lost precision

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 47

Page 48: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Option Explicit, Option Infer, and Option Strict (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 48

Figure 3-23: Option statements entered in the General Declarations section

Page 49: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Coding the Sunshine Cellular Application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49

Figure 3-24: Sunshine Cellular interface from Chapter 2

Page 50: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Coding the Sunshine Cellular Application (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50

Figure 3-25: Sunshine Cellular TOE chart from Chapter 2

Page 51: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Using Pseudocode to Plan a Procedure

• Pseudocode: short phrases that describe the steps a procedure needs to take to accomplish its goal

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 51

Page 52: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Using Pseudocode to Plan a Procedure (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 52

Figure 3-26: Pseudocode for the Sunshine Cellular application

Page 53: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Using a Flowchart to Plan a Procedure

• Flowchart: uses standardized symbols to show the steps a procedure must take to accomplish its goal

• Can be used in place of pseudocode for planning

• Three symbols:– Start/stop symbol (oval): indicates start and stop

points– Process symbol (rectangle): represents tasks– Input/output symbol (parallelogram): represents

input or output tasks

• Flowlines: connect the symbols to show the direction

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 53

Page 54: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 54

Figure 3-27: Flowcharts for the Sunshine Cellular application

Page 55: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 55

Coding the Calculate Order Button’s Click Event Procedure

Figure 3-28: Pseudocode for the calcButton’s Click event procedure

Page 56: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Coding the Calculate Order Button’s Click Event Procedure (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 56

Figure 3-29: Named constants and variables for the calcButton’s Click event procedure

Page 57: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Coding the Calculate Order Button’s Click Event Procedure (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 57

Figure 3-25: Declaration statements entered in the procedure

Page 58: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Coding the Calculate Order Button’s Click Event Procedure (cont'd.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 58

Figure 3-31: User input stored in variables

Page 59: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 59Figure 3-32: Completed calcButton’s Click event procedure

Page 60: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Completing the Sunshine Cellular Application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 60

• Empty string: set of quotation marks with nothing between them; also called zero-length string

• String.Empty: a value used to clear an object’s Text property

• Focus method: moves the focus to a specified control when the application is running

Figure 3-28: Pseudocode for the clearButton’s Click event procedure

Page 61: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 61

Figure 3-34: Sunshine Cellular application’s code

Page 62: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 62

Figure 3-34: Sunshine Cellular application’s code (cont'd.)

Page 63: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Testing and Debugging the Application

• To test an application:– Select a set of sample data– Manually compute the expected output– Run the application and compare its output with the

expected output

• Bug: an error in the program code

• Syntax error: an error that violates the programming language’s syntax – Usually caused by mistyping

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 63

Page 64: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Testing and Debugging the Application (cont'd.)

• Logic error: when the application does not perform as expected

• Valid data: data that the application is expecting

• Invalid data: data that is unexpected

• Test a program with both valid and invalid data

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 64

Page 65: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 65

Testing and Debugging the Application (cont'd.)

Figure 3-35: Sample test data for the Sunshine cellular application

Page 66: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 66

Testing and Debugging the Application (cont'd.)

Figure 3-36: Result of using the data from Set 1 in Figure 35

Page 67: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 67

Testing and Debugging the Application (cont'd.)

Figure 3-37: Result of using the data from Set 2 in Figure 3-35

Page 68: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 68

Formatting Numeric Output

• Formatting: specifying the number of decimal places and any special characters to display

• ToString method of a variable can be used to format a number

• FormatString argument: specifies the type of formatting to use

• Precision specifier: controls the number of significant digits or zeros to the right of the decimal point

Page 69: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 69

Figure 3-38: How to format a number

Page 70: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 70

Figure 3-39: The calcButton’s modified Click event procedure

Page 71: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 71

Formatting Numeric Output (cont'd.)

Figure 3-40: Formatted output shown in the interface

Page 72: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 72

Programming Tutorial 1

Figure 3-51: Result of entering 255, 255, and 0 as the RGB values

Page 73: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 73

Programming Tutorial 2

Figure 3-55: MainForm for the Vans & More Depot application

Page 74: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 74

Programming Example

Figure 3-63: Pseudocode

Page 75: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 75

Summary

• Variables and named constants are memory locations that store data

• Variables can change value, but constants cannot

• Variables and constants have a name, data type, initial value, scope, and lifetime

• Use Dim or Static to declare a variable at block or procedure level

• Use Private to declare a variable at class level

Page 76: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Summary (cont'd.)

• Assignment statement is used to assign values to an existing variable during run time

• Literals are constant items of data that do not change during run time

• String literal constants are enclosed in quotation marks

• Use the TryParse method to convert a string to a number

• The Convert class contains methods to convert values to a specified data type

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 76

Page 77: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Summary (cont'd.)

• A procedure-level variable is usable only by the procedure in which it is declared

• A class-level variable is usable by all procedures in the form

• A static variable is a procedure-level variable that retains its value even when the procedure ends

• Use comments to document your code

• Use Const to declare a named constant• Option Explicit On forces declaration of all

variables before use

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 77

Page 78: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 78

Summary (cont'd.)

• Option Infer Off warns if a variable declaration does not include a data type

• Option Strict On disallows any implicit type conversions that may cause a loss of data

• Pseudocode or a flowchart is used to plan a procedure’s code

• You can clear the contents of a text box or label control by assigning an empty string or String.Empty value

• The Focus method moves the focus to a control

Page 79: Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations

Summary (cont'd.)

• Test a program with both valid and invalid data

• Use the ToString method to format a program’s numeric output with special characters, such as for currency, percentages, and number of decimal places

Microsoft Visual Basic 2010: Reloaded, Fourth Edition 79