variables, constants, methods, and calculations chapter microsoft visual basic.net: reloaded 1

64
Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic .Net: Reloaded 1

Upload: merryl-ryan

Post on 19-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

Variables, Constants, Methods, and Calculations

Chapter Microsoft Visual Basic .Net: Reloaded

1

Page 2: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

2Microsoft Visual Basic .NET: Reloaded

Objectives

• Declare both a variable and a named constant

• Write an assignment statement

• Use the Convert class methods to convert data to an appropriate type

• Write arithmetic expressions

• Understand the scope of both a variable and a named constant

Page 3: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

3Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Include internal documentation in the code

• Use the Option Strict and Option Explicit statements

• Use a TOE chart to code an application

• Use pseudocode and a flowchart to plan an object’s code

• Send focus to a control

Page 4: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

4Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Explain the difference between syntax errors and logic errors

• Format an application’s numeric output

Page 5: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

5Microsoft Visual Basic .NET: Reloaded

Variables

• Computer memory locations where users can temporarily store data

• Must be assigned a data type by programmer

• Data type determines type of data variable can store

• Contents can change as application runs

Page 6: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

6Microsoft Visual Basic .NET: Reloaded

Selecting a Data Type for a Variable

Page 7: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

7Microsoft Visual Basic .NET: Reloaded

Selecting a Data Type for a Variable (continued)

• Integers whole numbers• Integer, Long, Short

• Floating-point numbers real numbers• Single, Double – use exponential notation

• Example: 32000 = 3.2E4

• Decimal uses fixed decimal point

• Character Types• Char – one character

• String – 0 to 2 billion characters

Page 8: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

8Microsoft Visual Basic .NET: Reloaded

Selecting a Name for a Variable

• Name should be descriptive

• Use three-letter prefix

• Use “m” for “module-scope” variables

• Punctuate using Pascal-case

• Type m and 3 letter prefix using lowercase, then capitalize the first letter of each word in the variable name

Page 9: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

9Microsoft Visual Basic .NET: Reloaded

Selecting a Name for a Variable (continued)

Page 10: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

10Microsoft Visual Basic .NET: Reloaded

Selecting a Name for a Variable (continued)

Page 11: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

11Microsoft Visual Basic .NET: Reloaded

Declaring a Variable

Page 12: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

12Microsoft Visual Basic .NET: Reloaded

Declaring a Variable (continued)• accessibility variablename as [datatype = initialvalue]

• accessibility - sets scope of variable

• Dim, Public, Private

• variablename = programmer chosen name

• datatype = data type of the variable

• Anything in brackets is optional

• initialvalue = default value at start of program

• If no value specified numeric types = 0, character types = Nothing, boolean = false

Page 13: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

13Microsoft Visual Basic .NET: Reloaded

Assigning Data to an Existing Variable

• assignment statement

• strName = “Mary”

• intAge = 35

• “=“ sign is called assignment operator

• Literal constant – value does not change

• Number 35 is numeric literal constant

• “Mary” is a string literal constant

• String - a group of characters enclosed in quotes

Page 14: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

14Microsoft Visual Basic .NET: Reloaded

Assigning Data to an Existing Variable (continued)

Page 15: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

15Microsoft Visual Basic .NET: Reloaded

Assigning Data to an Existing Variable (continued)

• Literal type characters force literal constants to assume new data type

Page 16: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

16Microsoft Visual Basic .NET: Reloaded

Using the Convert Class

• Convert class creates object that is either a number or a string by converting value to a specified data type and returning result strAge = Convert.ToString(intAge)

Page 17: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

17Microsoft Visual Basic .NET: Reloaded

Writing Arithmetic Expressions

• Precedence number indicates order in which VB performs operation in an expression

Page 18: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

18Microsoft Visual Basic .NET: Reloaded

Writing Arithmetic Expressions (continued)

• Integer division operator (\)

• Returns integer portion of division discarding remainder

• 8\3 results in answer of 2

• Modulus arithmetic operator (Mod)

• Returns remainder portion of division discarding integer portion

• 7 Mod 3 results in an answer of 1

• Often used to determine Leap years

• Valid only with division of integer values

Page 19: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

19Microsoft Visual Basic .NET: Reloaded

How To…

Page 20: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

20Microsoft Visual Basic .NET: Reloaded

The Scope and Lifetime of a Variable

• Scope

• Indicates where in the application’s code the variable can be used

• Lifetime

• Indicates how long the variable remains in the computer’s internal memory

Page 21: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

21Microsoft Visual Basic .NET: Reloaded

The Scope and Lifetime of a Variable (continued)

• Procedure Scope

• Procedure-level variable

• Variable declared within a procedure

• Variable can only be used in procedure in which declared

• Module scope

• Module-level variable

• Variable declared in General Declarations section of the form

• Variable can be used within all procedures in the form

Page 22: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

22Microsoft Visual Basic .NET: Reloaded

Procedure-level variables example

Page 23: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

23Microsoft Visual Basic .NET: Reloaded

Module-level variable example

Page 24: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

24Microsoft Visual Basic .NET: Reloaded

The Scope and Lifetime of a Variable (continued)

• Block scope

• Block-level variable

• Declared inside specific blocks of code

• If…Then…Else or For….Next

• Can be used only inside the block in which declared

Page 25: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

25Microsoft Visual Basic .NET: Reloaded

Named Constants• A memory location inside a computer whose

value cannot be changed while the application is running

Page 26: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

26Microsoft Visual Basic .NET: Reloaded

Named Constants (continued)

Page 27: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

27Microsoft Visual Basic .NET: Reloaded

Internally Documenting the Program Code

• Internal documentation:

• Term used by programmers for comments placed in code

• Place an apostrophe (‘) before text you want treated as internal documentation (comment)

• VB ignores everything after apostrophe on line

• Place comments at beginning of each procedure

Page 28: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

28Microsoft Visual Basic .NET: Reloaded

Internally Documenting the Program Code (continued)

• Include comments:

• as necessary to explain various sections of code

• at beginning of application explaining application’s:

• Name

• Purpose

• Author

• Date of creation or modification

Page 29: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

29Microsoft Visual Basic .NET: Reloaded

Internally Documenting the Program Code (continued)

Page 30: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

30Microsoft Visual Basic .NET: Reloaded

Option Explicit and Option Strict

• Option Explicit On• Tells computer to warn you if your code

contains name of an undeclared variable• Prevents unintentional declaration of variables

which automatically are of type Object• Option Strict On

• Tells computer not to perform any implicit type conversions which may lead to loss of data

• Implicit type conversions are automatic conversions from one data type to another performed by the computer to fit data into an assigned memory location

Page 31: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

31Microsoft Visual Basic .NET: Reloaded

Option Explicit and Option Strict (continued)

Page 32: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

32Microsoft Visual Basic .NET: Reloaded

Coding the SKATE-AWAY SALES Application

• Application calculates and displays total number of skateboards ordered and total price of skateboards including 5% sales tax

• Planning steps:• Identify tasks the application needs to perform

• Identify objects to which you will assign those tasks

• Identify events required to trigger an object into performing its assigned tasks

• Design the user interface

Page 33: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

33Microsoft Visual Basic .NET: Reloaded

Coding the SKATE-AWAY SALES Application (continued)

Page 34: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

34Microsoft Visual Basic .NET: Reloaded

Using Pseudocode to Plan a Procedure

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

Page 35: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

35Microsoft Visual Basic .NET: Reloaded

Using a Flowchart to Plan a Procedure

• Flowchart – uses standardized symbols to show steps procedure must follow to reach its goal

• Standard symbols utilized:• Flowlines - connection lines between symbols• Start/stop – ovals indicating starting and

ending points of procedure• Process – rectangles designating tasks such

as calculations• Input/Output – parallelogram designating input

and output tasks such as getting information from user

Page 36: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

36Microsoft Visual Basic .NET: Reloaded

Using a Flowchart to Plan a Procedure (continued)

Page 37: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

37Microsoft Visual Basic .NET: Reloaded

Using a Flowchart to Plan a Procedure (continued)

• Coding the one task of bntExit_Click event using either pseudocode or flowchart tools

Page 38: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

38Microsoft Visual Basic .NET: Reloaded

Coding the btnClear control’s Click Event Procedure

Page 39: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

39Microsoft Visual Basic .NET: Reloaded

Assigning a Value to the Property of a Control

• Zero-length string - “”• Also called empty string

• Set of quotation marks with nothing between them

• Assigned to the text property of a control at run-time removes contents of control

• Me.txtname.Text• Me refers to the current form

• txtName refers to a textbox control on the form

• Text refers to a property of the txtName control

Page 40: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

40Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 41: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

41Microsoft Visual Basic .NET: Reloaded

Using the Focus Method

• Focus indicates the control that can accept input and/or is awaiting an action from user

Page 42: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

42Microsoft Visual Basic .NET: Reloaded

btnClear_Click Event Procedure code

Page 43: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

43Microsoft Visual Basic .NET: Reloaded

Coding the btnCalc Control’s Click Event Procedure

Page 44: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

44Microsoft Visual Basic .NET: Reloaded

Coding the btnCalc Control’s Click Event Procedure (continued)

Page 45: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

45Microsoft Visual Basic .NET: Reloaded

Completed Code for Application

Page 46: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

46Microsoft Visual Basic .NET: Reloaded

Completed Code for Application (continued)

Page 47: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

47Microsoft Visual Basic .NET: Reloaded

Testing and Debugging the Application

• Invalid data - data the application is not expecting

• Debugging:• Process of locating syntax and logic errors in

the program • Syntax errors

• Typographical errors that violate rules governing valid syntax of the language

• Me.Colse() instead of Me.Close()• Logic errors

• Instruction that does not give expected results • decAverage = decNum1 + decNum2 / 2

Page 48: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

48Microsoft Visual Basic .NET: Reloaded

Testing and Debugging the Application (continued)

• First test with valid data

Page 49: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

49Microsoft Visual Basic .NET: Reloaded

Testing and Debugging the Application (continued)

• Then test with invalid data

Page 50: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

50Microsoft Visual Basic .NET: Reloaded

Formatting Numeric Output

• Formatting:

• Specifying number of decimal places and special characters to display in a number

• Format string:

• String used to specify format

• Example: “Axx” must be enclosed in quotes

• Format specifier - A

• Alphabetic character that specifies format you wish to use

• Precision specifier – xx

• xx is the sequence of digits desired

Page 51: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

51Microsoft Visual Basic .NET: Reloaded

Formatting Numeric Output (continued)

Page 52: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

52Microsoft Visual Basic .NET: Reloaded

Formatting Numeric Output (continued)

Page 53: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

53Microsoft Visual Basic .NET: Reloaded

Formatting Numeric Output (continued)

Page 54: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

54Microsoft Visual Basic .NET: Reloaded

Programming Example – Currency Calculator

• Application allows user to enter number of American dollars that he or she wants to convert to British pounds and Mexican pesos

• Make appropriate calculations and then display results on screen

Page 55: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

55Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 56: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

56Microsoft Visual Basic .NET: Reloaded

User Interface

Page 57: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

57Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 58: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

58Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 59: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

59Microsoft Visual Basic .NET: Reloaded

Pseudocode

btnExit Click event procedureclose application

btnCalc Click event procedure1. assign input value to a variable2. calculate number of British pounds by multiplying American dollars by .6268813. calculate number of Mexican pesos by multiplying American dollars by 10.3924.display number of British pound and Mexican pesos in lblBritish and lblMexican5.send the focus to the txtAmerican text box

Page 60: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

60Microsoft Visual Basic .NET: Reloaded

Code

Page 61: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

61Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 62: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

62Microsoft Visual Basic .NET: Reloaded

Summary

• Variables and named constants are computer memory locations that store data

• Variables are declared with a name and data type

• Scope refers to where a program can access a variable or named constant• Module scope variables can be accessed

anywhere in the module• Procedure scope variables can only be

accessed in the procedure in which declared• Block scope variables can only be accessed

within the block in which declared

Page 63: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

63Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Lifetime refers to how long a variable remains in the computer’s memory

• Literal constants are items of data• Convert class contains methods to convert

values to a specified data type• Integer division operator divides 2 integers

and returns the result discarding any remainder

• Modulus division operator divides 2 integers and returns the remainder

Page 64: Variables, Constants, Methods, and Calculations Chapter Microsoft Visual Basic.Net: Reloaded 1

64Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Option Explicit warns if undeclared variables are used

• Option Strict prevents implicit type conversions that may result in loss of data

• Use pseudocode (short phrases) or flowcharts (standardized symbols) to plan the application

• Test application with both valid and invalid data

• Format numeric output to display special characters