Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.

Slides:



Advertisements
Similar presentations
Chapter 3: Using Variables and Constants
Advertisements

Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 2: Designing Applications
1.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 04.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 2: Introduction to C++.
Microsoft Visual Basic: Reloaded Chapter Three Memory Locations and Calculations.
Basic Elements of C++ Chapter 2.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
CHAPTER THREE Representing Data: Constants and Variables.
Microsoft Visual Basic 2005 BASICS Lesson 4 Mathematical Operators.
Chapter 12: How Long Can This Go On?
Input, Output, and Processing
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CHAPTER THREE Representing Data: Constants and Variables.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
A variable is a name for a value stored in memory.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Data Types, Arithmetic Operations
Basic Elements of C++.
Variables and Arithmetic Operations
Chapter 3: Using Variables and Constants
The Selection Structure
Basic Elements of C++ Chapter 2.
Variables and Arithmetic Operations
Objectives After studying this chapter, you should be able to:
CIS16 Application Development Programming with Visual Basic
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
An Introduction to Programming with C++ Fifth Edition
Presentation transcript:

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations

Microsoft Visual Basic 2005: Reloaded, Second Edition2 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 2005: Reloaded, Second Edition3 Objectives (continued) Understand the purpose of the Option Explicit, Option Strict, and Imports statements Use a TOE chart, pseudocode, and a flowchart to code an application Clear the contents of a control’s Text property while an application is running

Microsoft Visual Basic 2005: Reloaded, Second Edition4 Objectives (continued) Send the focus to a control while the application is running Explain the difference between syntax errors and logic errors Format an application’s numeric output

Microsoft Visual Basic 2005: Reloaded, Second Edition5 Variables Variables: computer memory locations used to store data while an application is running Every variable has a: –Name –Data type –Scope –Lifetime

Microsoft Visual Basic 2005: Reloaded, Second Edition6 Selecting a Data Type for a Variable Each variable must be assigned a data type Data type: the type of data the variable can store Each data type is a class Unicode: –Universal coding scheme for characters –Assigns a unique numeric value to each character

Microsoft Visual Basic 2005: Reloaded, Second Edition7 Selecting a Data Type for a Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition8 Selecting a Name for a Variable Identifier: descriptive name given to a variable Use a meaningful name that reflects the purpose of the variable Use camel casing for variable identifiers Variable names must conform to naming rules

Microsoft Visual Basic 2005: Reloaded, Second Edition9 Selecting a Name for a Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition10 Declaring a Variable Declaration statement: used to declare, or create, a variable Declaration statement includes –Scope keyword: Dim or Private or Static –Name of the variable –Data type –Initial value (optional)

Microsoft Visual Basic 2005: Reloaded, Second Edition11 Declaring a Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition12 Assigning Data to an Existing Variable Assignment statement: –Used to assign values to properties of controls –Used to assign values to variables Assignment operator: (=) –Value on the right of the = operator is assigned to the variable on the left of the = operator

Microsoft Visual Basic 2005: Reloaded, Second Edition13 Assigning Data to an Existing Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition14 Assigning Data to an Existing Variable (continued) String: group of characters enclosed in quotation marks 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 with a decimal place is treated as a Double type Literal type character: forces a literal constant to assume a specific data type

Microsoft Visual Basic 2005: Reloaded, Second Edition15 Assigning Data to an Existing Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition16 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 TryParse method has 4 arguments –String: string value to be converted –Variable: location to store the result –IFormatProvider (optional): specifies formatting –NumberStyles (optional): allows formatting characters to be in the data to be converted

Microsoft Visual Basic 2005: Reloaded, Second Edition17 Using the TryParse Method (continued) IFormatProvider argument formats numbers, dates, and times NumberFormatInfo.CurrentInfo value: –Uses the formatting characters specified in the Windows Customize Regional Options dialog box

Microsoft Visual Basic 2005: Reloaded, Second Edition18 Using the TryParse Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition19 Using the TryParse Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition20 Using the TryParse Method (continued) Assign the TryParse method’s return value to a Boolean variable –If True, the conversion was successful –If False, the value could not be converted Line continuation character: the underscore (_) –Breaks up a long instruction into two or more lines –Must appear at end of line, preceded by a space Must have an Imports statement in the General Declarations section of code to use NumberStyles and NumberformatInfo.CurrentInfo : –Imports System.Globalization

Microsoft Visual Basic 2005: Reloaded, Second Edition21 Using the TryParse Method (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition22 Using the Convert Class Convert class: –Contains methods for converting numeric values to specific data types Use the dot member access operator to separate the class name from the method name

Microsoft Visual Basic 2005: Reloaded, Second Edition23 Using the Convert Class (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition24 Writing Arithmetic Expressions 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

Microsoft Visual Basic 2005: Reloaded, Second Edition25 Writing Arithmetic Expressions (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition26 Writing Arithmetic Expressions (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition27 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 are usually declared in two places: –Within a procedure –In the form’s Declarations section Procedure-level variable: declared within a procedure Procedure scope: only the procedure can use the variable

Microsoft Visual Basic 2005: Reloaded, Second Edition28 The Scope and Lifetime of a Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition29 The Scope and Lifetime of a Variable (continued) 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 2005: Reloaded, Second Edition30 The Scope and Lifetime of a Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition31 The Scope and Lifetime of a Variable (continued) Module scope: variable can be used by all procedures in the form Module-level variable: –Declared in the form’s Declarations section –Use Private keyword in declaration Module-level variables retain their values until the application ends

Microsoft Visual Basic 2005: Reloaded, Second Edition32 The Scope and Lifetime of a Variable (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition33 The Scope and Lifetime of a Variable (continued) Block scope: variable can be used within a specific block of code Block-level variable: declared within a specific block of code

Microsoft Visual Basic 2005: Reloaded, Second Edition34 Static Variables Static variable: –Procedure-level variable that retains its value even after the procedure ends –Retains its value until the application ends –Can be used instead of a module-level variable A static variable has: –Same lifetime as a module-level variable –Narrower scope than a module-level variable Declared using the Static keyword

Microsoft Visual Basic 2005: Reloaded, Second Edition35 Static Variables (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition36 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

Microsoft Visual Basic 2005: Reloaded, Second Edition37 Named Constants (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition38 Named Constants (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition39 Named Constants (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition40 Option Explicit and Option Strict Option Explicit: –When on, all variables used must first be declared –Protects against misspelled variable names in code –Placed in the General Declarations section of code editor Implicit type conversion: can occur if the value on the right side of an assignment statement is not the same data type as the variable on the left side

Microsoft Visual Basic 2005: Reloaded, Second Edition41 Option Explicit and Option Strict (continued) Promoting: when a value is converted to another data type that stores larger numbers Demoting: when a value is converted to another data type that stores only smaller numbers –Data loss can occur with demoting Option Strict: –Can be used to enforce correct data typing –Placed in the General Declarations section of the code editor

Microsoft Visual Basic 2005: Reloaded, Second Edition42 Option Explicit and Option Strict (continued) Option Strict On follows these conversion rules: –Strings are not implicitly converted to numbers or vice versa –Narrower data types are implicitly promoted to wider data types –Wider data types are not implicitly demoted to narrower data types

Microsoft Visual Basic 2005: Reloaded, Second Edition43 Option Explicit and Option Strict (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition44 Coding the Skate-Away Sales Application

Microsoft Visual Basic 2005: Reloaded, Second Edition45 Coding the Skate-Away Sales Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition46 Using Pseudocode to Plan a Procedure Pseudocode: short phrases to describe the steps a procedure needs to take to accomplish its goal

Microsoft Visual Basic 2005: Reloaded, Second Edition47 Using Pseudocode to Plan a Procedure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition48 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

Microsoft Visual Basic 2005: Reloaded, Second Edition49 Using a Flowchart to Plan a Procedure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition50 Using a Flowchart to Plan a Procedure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition51 Coding the clearButton’s Click Event Procedure

Microsoft Visual Basic 2005: Reloaded, Second Edition52 Clearing the Contents of a Control’s Text Property

Microsoft Visual Basic 2005: Reloaded, Second Edition53 Clearing the Contents of a Control’s Text Property (continued) Zero-length string (or empty string): –Removes the contents in the Text property of a control –Use empty set of quotation marks: “” String.Empty: used to assign an empty string to a control’s Text property For TextBox control, use the Clear method

Microsoft Visual Basic 2005: Reloaded, Second Edition54 Setting the Focus Focus method: moves the focus to a specified control at runtime

Microsoft Visual Basic 2005: Reloaded, Second Edition55 Setting the Focus (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition56 Setting the Focus (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition57 Coding the calcButton’s Click Event Procedure

Microsoft Visual Basic 2005: Reloaded, Second Edition58 Coding the calcButton’s Click Event Procedure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition59 Coding the calcButton’s Click Event Procedure (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition60 Testing and Debugging the Application Valid data: data that the application is expecting Invalid data: data that is unexpected Debugging: locating errors in a program Syntax errors: usually caused by mistyping Logic errors: occur when you enter an instruction that does not give the expected results Test a program with both valid and invalid data

Microsoft Visual Basic 2005: Reloaded, Second Edition61 Testing and Debugging the Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition62 Testing and Debugging the Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition63 Formatting Numeric Output Formatting: specifying the number of decimal places and any special characters to display Format specifier: specifies the type of formatting to use Precision specifier: controls the number of significant digits or zeros to the right of the decimal point

Microsoft Visual Basic 2005: Reloaded, Second Edition64 Formatting Numeric Output (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition65 Formatting Numeric Output (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition66 Formatting Numeric Output (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition67 Formatting Numeric Output (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition68 Formatting Numeric Output (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition69 Programming Tutorial

Microsoft Visual Basic 2005: Reloaded, Second Edition70 Programming Example

Microsoft Visual Basic 2005: Reloaded, Second Edition71 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, scope, and lifetime Use Dim to declare a variable at block or procedure level Use Private to declare a variable at module level

Microsoft Visual Basic 2005: Reloaded, Second Edition72 Summary (continued) Assignment statement is used to assign values to an existing variable Literals are constant items of data Use the TryParse method to convert a string to a number Use the Imports statement to import a namespace The Convert class contains methods to convert values to a specified data type

Microsoft Visual Basic 2005: Reloaded, Second Edition73 Summary (continued) A procedure-level variable is usable only by the procedure in which it is declared A module-level variable is usable by all procedures in the form A block-level variable is usable only within the block in which it is declared A static variable is a procedure-level variable that retains its value when the procedure ends Option Explicit On forces declaration of all variables before use

Microsoft Visual Basic 2005: Reloaded, Second Edition74 Summary (continued) 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 Use the Clear method or empty string to clear a textbox The Focus method moves the focus to a control Test a program with both valid and invalid data