Microsoft Visual Basic 2005: Reloaded

Similar documents
SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

Microsoft Visual Basic 2015: Reloaded

Programming Language 2 (PL2)

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Full file at

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

The PCAT Programming Language Reference Manual

Introduction to Data Entry and Data Types

Outline. Data and Operations. Data Types. Integral Types

Visual C# Instructor s Manual Table of Contents

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

Java Primer 1: Types, Classes and Operators

VB FUNCTIONS AND OPERATORS

Chapter 2: Using Data

X Language Definition

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CS313D: ADVANCED PROGRAMMING LANGUAGE

Declaration and Memory

The SPL Programming Language Reference Manual

Object Oriented Programming with Visual Basic.Net

FRAC: Language Reference Manual

BASIC ELEMENTS OF A COMPUTER PROGRAM

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

Visual BASIC Creating an Application. Choose File New Project from the menu

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Introduction to Programming Using Java (98-388)

CSE 123 Introduction to Computing

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Introduction to C# Applications

Typescript on LLVM Language Reference Manual

Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

IT 374 C# and Applications/ IT695 C# Data Structures

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Bit (0, 1) Byte (8 bits: 0-255) Numeral systems. Binary (bin) 0,1 Decimal (dec) 0, 1, 2, 3, Hexadecimal (hex) 0, 1, 2, 3, 1/13/2011

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Lecture 2 Tao Wang 1

Lecture 3 Tao Wang 1

egrapher Language Reference Manual

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

ARG! Language Reference Manual

Chapter 2: Using Data

LECTURE 02 INTRODUCTION TO C++

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CSC Web Programming. Introduction to JavaScript

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Expr Language Reference

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Data Types. Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions

Chapter 2: Basic Elements of C++

Agenda & Reading. VB.NET Programming. Data Types. COMPSCI 280 S1 Applications Programming. Programming Fundamentals

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Full file at C How to Program, 6/e Multiple Choice Test Bank

Microsoft Visual Basic 2005: Reloaded

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Language Fundamentals

A Beginner s Guide to Programming Logic, Introductory. Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

GraphQuil Language Reference Manual COMS W4115

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C: How to Program. Week /Mar/05

C++ Programming: From Problem Analysis to Program Design, Third Edition

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Data and Operations. Outline

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Reserved Words and Identifiers

Model Viva Questions for Programming in C lab

JAVA Programming Fundamentals

Full file at

Vba Variables Constant and Data types in Excel

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

2 nd Week Lecture Notes

Information Science 1

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Programmers should write code that is self-documenting and split into small sections.

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

Introduction to Programming

Computers Programming Course 6. Iulian Năstac

1 Lexical Considerations

GeoCode Language Reference Manual

Chapter 2. Lexical Elements & Operators

BITG 1233: Introduction to C++

Petros: A Multi-purpose Text File Manipulation Language

Creating a C++ Program

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Chapter 2 - Introduction to C Programming

Operators and Expressions

VBScript: Math Functions

Program Fundamentals

used for US Census; Holes were made to represent information to be tabulated were punched in cards; Successful

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 1. The built-in value types (continued)

CS 211 Programming Practicum Fall 2018

Introduction to Computer Use II

1.3b Type Conversion

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Transcription:

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, 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 2005: Reloaded, Second Edition 2

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 Edition 3

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 Edition 4

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 Edition 5

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 Edition 6

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

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 Edition 8

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

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 Edition 10

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

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 Edition 12

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

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 Edition 14

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

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 Edition 16

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 Edition 17

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

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

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 Edition 20

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

Convert class: Using the 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 Edition 22

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

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 Edition 24

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

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

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 Edition 27

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

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 Edition 29

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

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 Edition 31

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

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 Edition 33

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 Edition 34

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

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 Edition 36

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

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

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

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 Edition 40

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 Edition 41

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 Edition 42

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

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

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

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 Edition 46

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

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 Edition 48

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

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

Coding the clearbutton s Click Event Procedure Microsoft Visual Basic 2005: Reloaded, Second Edition 51

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

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 Edition 53

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

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

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

Coding the calcbutton s Click Event Procedure Microsoft Visual Basic 2005: Reloaded, Second Edition 57

Coding the calcbutton s Click Event Procedure (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 58

Coding the calcbutton s Click Event Procedure (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 59

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 Edition 60

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

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

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 Edition 63

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

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

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

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

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

Programming Tutorial Microsoft Visual Basic 2005: Reloaded, Second Edition 69

Programming Example Microsoft Visual Basic 2005: Reloaded, Second Edition 70

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 Edition 71

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 Edition 72

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 Edition 73

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