Top Banner
3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic
43

3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

Dec 27, 2015

Download

Documents

Buddy Clarke
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-1

Chapter 3

Variables, Assignment Statements, and Arithmetic

Page 2: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-2

Learning Objectives

Understand a four-step process to writing code in VB .NET and its relationship the six operations a computer can perform

Declare and use different types of variables in your project

Describe the various type of data used in a program

Use text boxes for event-driven input and output

Page 3: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-3

Learning Objectives (continued)

Write VB .NET functions to carry out commonly used operations

Use buttons to clear text boxes and exit the project

Describe the type of errors that commonly occur in a VB .NET project and their causes

Page 4: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-4

Four step coding process

Once the logic is defined through IPO tables or pseudocode, one needs to:

Decide what variables and constants will be needed and declare them

Input data from text boxes or other input controls Process data into information using arithmetic or

string operations and store results in variables Output values of variables to text boxes or other

output controls

Page 5: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-5

Variables

Conceptually a variable is a space in memory with a Name and a Value.

The name identify the variable itself. It does not change.

The value can be changed at will, i.e. it can be written to as well as read from.

Name Value

Page 6: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-6

Naming Variables

A variable name can contain– Letters, A-Z– Digits, 0-9– The underscore _

A variable name must start with a letter Variable names are case insensitive

– MyVariable and myvariable refer to the same variable

Page 7: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-7

Variable Names

Cannot be any of VB .NET keywords Preferably meaningful names Often follows some convention to indicate

– The type of the variable– The scope of the variable– Perhaps other characteristic

Page 8: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-8

Commonly Used Data Types

String : to hold text Single : a real number Double : a real number with larger coverage and

precision than the single Integer : an integer Long : an integer with larger coverage Decimal : to work with currency Date : a date Boolean : True or False

Page 9: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-9

Data types

Page 10: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-10

Declaring Variables

Purpose : informing VB .NET of the type of the variable

Typical declaration:– Dim variableName As variable Type

Examples– Dim dblWeight As Double– Dim strName As String– Dim x As Long, y As Long

Page 11: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-11

Declare two variables of same type– Dim x, y As Long– Dim c As Integer, x, y As Long– In both cases, both x and y are Long

Initialization and declaration– Dim intAge As Integer = 10– Dim decPrice As Decimal = 100.00

Page 12: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-12

Option Explicit Statement

Use one of the following to enforce the requirement of declaring every variable you use (or not)– Option Explicit On– Option Explicit Off

By default Option Explicit is On The Statement should be at the very top of

the module

Page 13: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-13

Event-Driven Input

You need data to operate on You need a way to “input” the data You typically use controls such as text boxes

to take the input You read the value of the input in code when

a certain event occurs

Page 14: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-14

TextBox Control

Allows the user to enter text Can also be used to display text The Text property of the control is always

equal to the text entered in the box

Page 15: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-15

A simple calculator

A simple application where– User enters two numbers in TextBoxes– User clicks a Sum button– The application displays the sum in another

TextBox

Page 16: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-16

Calculator appearance

Page 17: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-17

Control properties

Page 18: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-18

Assignment Statement

Declare variables to hold useful values– Dim intFirst, intSecond, intSum As Integer

When user clicks the sum button, read and assign the values in each TextBox to the right variable– intFirst = txtFirstNum.Text– intSecond = txtSecondNum.Text

Assignment : VariableName = SomeValue

Page 19: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-19

Tip: Tab Order

A power user can use the Tab key to navigate controls in an application

The order of the controls depends on the order in which they are laced on the form

To change the default order– Change the TabIndex property of controls– Use the View | Tab Order menu

Page 20: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-20

Functions

A function is a black box taking some input (arguments) and returning a value

Page 21: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-21

Example of Functions

CInt() : converts the argument to integer

Today() : returns the system date

CStr() : converts a number to a string– Dim MyString as String, MyInt as Integer = 12– MyString = CStr( MyInt)– MyString holds the value “12”, not 12

Page 22: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-22

Code for btnSum

Private Sub btnSum_Click(ByVal sender as System.Object, _ ByVal e as System.EventArgs) _Handles btnSum.Click

Dim intFirst, intSecond, intSum As Integer

intFirst = txtFirstNum.Text intSecond = txtSecondNum.Text intSum = intFirst + intSecond txtSum.text = str(intSum)

End Sub

Page 23: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-23

Properties and Methods

Access properties and methods through the dot notation– object.propertyName– object.methodName

Ex: The Focus method shifts the cursor to the object calling it– txtFirstNum.Focus()

Page 24: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-24

Step-By-Step 3-1 Creating calculator

Demo

Page 25: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-25

Arithmetic Operators

Page 26: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-26

Hierarchy of operations

The order in which operations are executed is the following

1. Parentheses2. Raising to a power3. Change of sign (negation)4. Multiplication or division5. Integer division6. Modulus7. Addition or subtraction

Page 27: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-27

String Operators

The only string operator is the concatenation operator, &

It combines two strings into one– Dim strFName as String = “Jesse”– Dim strLName as String = “James”– Dim strName as String– strName = strFName & “ “ & strLName

Page 28: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-28

Symbolic Constants

Rather than have “magic” numbers in your programs, use symbolic constants

Ex: you want to find the total sale when adding sales tax (now 7%)

Declare a Constant and use it in code– Const TaxRate as Double = 0.07

If you maintain the program later and the tax rate has changed you need to reflect the change in a single place

Page 29: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-29

Vintage DVDs

Extend application. Allow to input renter’s name, the DVD rented, and price for DVD.

Allow for printing rental price with tax, clearing the textboxes, and exiting application.

Page 30: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-30

Step-by-Step 3-2 Modifying a project

Demo

Page 31: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-31

Develop logic for action objects

Page 32: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-32

Write and test code

Code for Calculate button

Private Sub btnCalc_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCalc.Click

Const sngTaxRate As Single = 0.07 'Use local tax rate Dim decPrice, decAmountDue, decTaxes As Decimal decPrice = CDec(txtDVDPrice.Text)

decTaxes = decPrice * sngTaxRate 'Compute taxes decAmountDue = decPrice + decTaxes 'Compute amount due

txtTaxes.Text = CStr(decTaxes) txtAmountDue.Text = CStr(decAmountDue)

End Sub

Page 33: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-33

Conversion functions

Page 34: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-34

Formatting Output

Formatting functions– Format( expression, format )– FormatCurrency( expression )

Page 35: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-35

Format Expressions

Page 36: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-36

Clearing Entries on the Form

Private Sub btnClear_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs)

_Handles btnClear.Click

txtCustName.Text = "" txtDVDName.Text = "" txtDVDPrice.Text = "" txtTaxes.Text = "" txtAmountDue.Text = "" txtCustName.Focus()

End Sub

Page 37: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-37

Step-by-Step 3-3 Adding controls to Vintage DVDs

Demo

Page 38: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-38

More built-in functions

Page 39: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-39

Monthly payment Calculator

Page 40: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-40

Code for btnCompute_Click

Private Sub btnCompute_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) _ Handles btnCompute.Click

Dim decAmount, decPayment As Decimal Dim intMonths As Integer Dim sngRate As Single decAmount = CDec(txtAmount.Text) intMonths = CInt(txtMonths.Text) sngRate = (CSng(txtRate.Text) / 100) / 12 decPayment = Pmt(sngRate, intMonths, -decAmount) txtPayment.Text = Format(decPayment, "currency") txtAmount.Text = Format(decAmount, "currency")

End Sub

Page 41: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-41

Ste-by-Step 3-4 Creating Monthly Payment Calculator

Demo

Page 42: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-42

Errors in VB.Net

Syntax Errors– Incorrect grammar– Incorrect Usage

Runtime Errors– Occurs while program is running– Caused by un planned conditions

division by 0

Logic Errors– Incorrect program design

Page 43: 3-1 Chapter 3 Variables, Assignment Statements, and Arithmetic.

3-43

Copyright 2004 John Wiley & Sons, Inc.

All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein