Programming with Microsoft Visual Basic th Edition

Slides:



Advertisements
Similar presentations
Programming with Microsoft Visual Basic th Edition
Advertisements

Chapter 3: Using Variables and Constants
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 11: Classes and Objects
1.
String Variables Visual Basic for Applications 4.
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
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.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic: Reloaded Chapter Three Memory Locations and Calculations.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
Chapter 8: String Manipulation
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
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.
Chapter 3 Part 11 Variables, Constants, and Calculations Chapter 3 Section 3.3 Chapter 3 Section 3.4.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Representing Data: Constants and Variables CHAPTER THREE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 5: More on the Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 7 Where Can I Store This?
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
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.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
CHAPTER THREE Representing Data: Constants and Variables.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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.
Representing Data: Constants and Variables
Egyptian Language School Computer Department
Chapter 9: Value-Returning Functions
A variable is a name for a value stored in memory.
VBScript Session 1 IT Professional Academy.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
Chapter 3: Using Variables and Constants
The Selection Structure
Variables and Arithmetic Operations
Microsoft Visual Basic 2005 BASICS
Chapter 6 Variables What is VBScript?
CIS16 Application Development and Programming using Visual Basic.net
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
An Introduction to Programming with C++ Fifth Edition
Presentation transcript:

Programming with Microsoft Visual Basic 2010 5th Edition Chapter Three Using Variables and Constants

Previewing the Modified Playtime Cellular Application Previewing the Playtime Cellular application Access Run command on Start menu Browse to VB2010\Chap03 folder Click the Playtime Cellular (Playtime Cellular.exe) file View order form Enter customer information from pages 119-120 Completed application resembles Chapter 2 version

Figure 3-2 Completed order form

Lesson A Objectives After studying Lesson A, you should be able to: Declare variables and named constants Assign data to an existing variable Convert string data to a numeric data type using the TryParse method Convert numeric data to a different data type using the Convert class methods

Lesson A Objectives (cont’d.) Explain the scope and lifetime of variables and named constants Explain the purpose of the Option Explicit, Option Infer, and Option Strict

Using Variables to Store Information Controls and variables temporarily store data Variable: Temporary storage location in main memory Specified by data type, name, scope, and lifetime Reasons to use variables Hold information that is not stored in control on form Allow for more precise treatment of numeric data Enable code to run more efficiently

Using Variables to Store Information (cont’d.) Selecting a data type for a variable Data type: Specifies type of data a variable can store Provides a class template for creating variables Unicode Universal coding scheme for characters Assigns unique numeric value to each character in the written languages of the world

Basic data types in Visual Basic Figure 3-3 Basic data types in Visual Basic

Using Variables to Store Information (cont’d.) For this course: Use Integer data type for all integers Use either Decimal or Double data type for numbers containing decimal places or numbers used in calculations Use String data type for text or numbers not used in calculations Use Boolean data type for Boolean values

Using Variables to Store Information (cont’d.) Selecting a name for a variable Variables are referred to by name Identifier: Another term for variable name Guidelines for naming variables Use Hungarian notation, with a three-character prefix representing the variable’s data type Name should be descriptive: e.g., dblLength Use camel case: e.g., dblSalesAmount Must follow variable naming rules

Figure 3-4 Variable naming rules and examples

Using Variables to Store Information (cont’d.) Declaring a variable Declaration statement: Used to declare (create) a variable and reserve space in memory for it Syntax shown in Figure 3-5 on next slide If no initial value is given to variable when declaring it, computer stores default value Numeric variables are set to 0 Boolean variables are set to False Object and String variables are set to Nothing Date variables are set to 1/1/0001 12:00:00AM

Figure 3-5 Syntax and examples of a variable declaration statement

Assigning Data to an Existing Variable Assignment statement: Assigns value to variable at run time Syntax: variablename = expression Expression may include literal constants, object properties, variables, keywords, arithmetic operators Literal constant Data item whose value does not change Example: The string “Mary” Literal type character Forces literal constant to change data type

Figure 3-6 Assignment statements in which the value’s data type matches the variable’s data type

Assigning Data to an Existing Variable (cont’d.) TryParse method: Converts string to number TryParse is preferred over Val Allows programmer to specify data type Val only returns a type Double value Syntax shown in Figure 3-7 on next slide dataType: Numeric data type, such as Integer string : String to be converted variable : Variable that receives the numeric value

Figure 3-7 Basic syntax and examples of the TryParse method

Assigning Data to an Existing Variable (cont’d.) Convert class: Can be used to convert a number from one type to another Syntax shown in Figure 3-9 on next slide Convert: Name of class method: Converts value to specified data type value: Numeric data to be converted TryParse is recommended for converting strings to numeric data types Will not produce an error if conversion fails

Figure 3-9 Syntax and examples of the Convert class methods

The Scope and Lifetime of a Variable Scope: Indicates where variable can be used Lifetime: How long variable remains in memory Scope and lifetime are determined by where variable is declared Three types of scope Class: Variable can be used by all procedures in a form Procedure: Variable can be used within procedure Block: Variable can be used within specific code block

The Scope and Lifetime of a Variable (cont’d.) Variables with procedure scope Can be used only by that procedure Declared at beginning of procedure Removed from memory when procedure ends Declared using Dim keyword Most variables used in this course will be procedure-level variables Sales tax example UI and code given on following slides illustrate use of procedure variables

The Scope and Lifetime of a Variable (cont’d.) Figure 3-10 User interface for the Sales Tax Calculator application

Click event procedures using procedure-level variables Figure 3-11 Click event procedures using procedure-level variables

The Scope and Lifetime of a Variable (cont’d.) Variables with class scope Can be used by all procedures in a form Declared in form’s Declarations section Remain in memory until application ends Declared using Private keyword Total Sales example UI and code given on following slides illustrate use of class-level variables

The Scope and Lifetime of a Variable (cont’d.) Figure 3-12 User interface for the Total Sales application

Figure 3-13 Code using a class-level variable

Static Variables Static variable: Procedure-level variable with extended lifetime Remains in memory between procedure calls Retains its value even when the procedure ends Static keyword: Used to declare static variable Static variables act like class-level variables but have narrower scope Can only be used within procedure where declared

Figure 3-14 Code using a static variable

Named Constants Named constant Const statement: Creates named constant Memory location inside computer whose contents cannot be changed at run time Const statement: Creates named constant Stores value of expression in named constant expression: Can be literal constant, another named constant, or an arithmetic operator Cannot contain a variable or method Syntax and examples shown in Figure 3-15 on next slide

Figure 3-15 Syntax and examples of the Const statement

Figure 3-16 User interface for the Area Calculator application Figure 3-17 Calculate Area button’s Click event procedure

Option Explicit, Option Infer, and Option Strict Option Explicit On statement Prevents you from using undeclared variables Implicit type conversion: Converts right-side value to the data type of left side Promotion Data converted to greater precision number e.g., Integer to Decimal Demotion Data truncated e.g., Decimal to Integer Data loss can occur when demotion occurs

Option Explicit, Option Infer, and Option Strict (cont’d.) Option Infer Off statement: Ensures that every variable is declared with a data type Option Strict On statement: Disallows implicit conversions Type conversion rules are applied when this option is on Figure 3-18 on following slide contains examples

Figure 3-18 Rules and examples of type conversions

Option Explicit, Option Infer, and Option Strict (cont’d.) Figure 3-19 Option statements entered in the General Declarations section

Lesson A Summary Declare a variable using {Dim | Private | Static} Assignment statement: Assigns value to a variable Three levels of scope: Block, procedure, class TryParse () converts strings to numeric data Use Const to declare a named constant Avoid programming errors by using Option Explicit On, Option Infer Off, and Option Strict On