Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.

Similar presentations


Presentation on theme: "Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic."— Presentation transcript:

1 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Chapter 3: Variables, Assignment Statements, and Arithmetic

2 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 2 Learning Objectives Declare and use different types of variables in your project. Use text boxes for event-driven input and output. Use a four-step process to write code for event procedures Write Visual Basic instructions to carry out arithmetic operations. Describe the hierarchy of operations for arithmetic. Understand how to store the result of arithmetic operations in variables using assignment statements. Use comments to explain the purpose of program statements. Discuss using Visual Basic functions to carry out commonly used operations. Use command buttons to clear text boxes, print a form with the PrintForm method, and exit the project. Describe the types of errors that commonly occur in a Visual Basic project and their causes.

3 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Variables Variables are named locations in memory (memory cells) in which we store data that will change at run time Variable names in VB: –Must begin with letter –can’t include period –can’t be over 255 characters –are not case-sensitive Example variable names: –VideoPrice for Video Rental Price –Taxes for taxes on this price –AmountDue for sum of price and taxes –YTDEarnings for year to date earnings –EmpLastName for employee’s last name

4 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Data Types Two primary types of data: numeric and string Numeric data can be used in arithmetic operations –3.154; 2300; -34; 0.0000354 are all numeric constants String data should not be used in arithmetic – “Dogs”; “123-45-6789”; “Dr. Brown” are all string constants Numeric data types can be subdivided into specific types: - currency$55,567.78- integer255 - single 567.78- long (integer) 35,455 - double 567.78129086- BooleanTrue or False

5 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Declaring Variables Declare ALL variables with the DIM statement General form: –Dim Variable1 as type1, variable2 as type2, etc. For example, –Dim strMyName as String, sngMyValue as Single –Dim curTaxes as Currency, curPrice as Currency –Dim curAmountDue as Currency Two or more variables can be declared with same Dim statement but you must include the variable type If you fail to declare a variable after the Option Explicit statement has been entered, an error occurs at Run time Use variables rather than objects in processing since all textboxes are strings

6 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors The Option Explicit Statement Begin ALL Forms with the Option Explicit command in the declarations procedure of the general object. This forces all variables to be declared To automatically include Option Explicit, go to the Tools|Options|Editor menu selection and check the box for “Require variable declaration”

7 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 Event-driven Input In VB, we often use event-driven input where data is transferred from text boxes to variables by an event Use an assignment statement to do this: Control property or variable = value, variable, or property Only variables or control properties can be on left of assignment statement

8 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Assignment Statements Must convert strings in text boxes to numeric with Val function, e.g., –Price = Val(txtPrice.Text) Convert numeric variables to strings before assigning to text box, e.g., –txtAmountDue.Text = Str(curAmountDue) Carry out calculations with assignment statements, e.g., –curTaxes = 0.07*curPrice –curAmountDue = curPrice + curTaxes

9 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Using Functions Sometimes we want to convert a string to a number or vice versa or to compute a single value To do this, we use a function that is nothing more than an operation that takes a multiple arguments and generates a single value variable = functionName(arg1, arg2, …) Example functions for converting data: –Val to convert a string to a number –Str to convert a number to a string –CCur to convert a string to a currency data type

10 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 Using Assignment Statements for Calculations Sometimes, an expression will be on right of assignment statement An expression is a combination of one or more variables and/or constants with operators Operators are symbols used for carrying out processing

11 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 Arithmetic Operators () for grouping+ for addition ^ for exponentiation- for subtraction - for negation * for multiplication / for division \ for integer division mod for modulus

12 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Hierarchy of Operations Operations within parentheses ( ) Exponentiation (^) Negation (-) Multiplication and division (*, /) Integer division (\) Modulo arithmetic (Mod) Addition and subtraction (+, -) String concatenation (&)

13 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Arithmetic Example 3 * (curSalary - curTaxes)^2 - curBonus/curMonths 3 1 2 5 4 Order 1Subtract Taxes from Salary 2Square the result 3Multiply this result by 3 4Divide Bonus by Months 5Subtract result from first expression

14 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 Symbolic Constants We can assign a name to a constant with the Const statement const constant name as variable type = value Example Const sngRate As Single = 0.07

15 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 Comments To explain the purpose of a statement, a comment statement is added Any statement beginning with an apostrophe or REM is a comment Comments can be added to end of statements using apostrophe

16 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Formatting Data To display information in a pleasant form, we can use the Format function: variable or control = Format(variable, format expression) Where the format expressions are in quotes and include; –Currency –Fixed –Standard –Percent –Scientific Example: txtTaxes.Text = Format(curTaxes, “currency”)

17 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 17 Print Form, Clearing Entries and Setting Focus To print the form, use the PrintForm command To clear a text box, set it equal to the null string ”” (no space) To set the focus to a text box, use the Setfocus method For example, txtCustName.SetFocus sets focus to this textbox

18 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Using Other Arithmetic Functions Other useful functions include –Abs for absolute value Sqr for square root –FV for future value PV for present value –IRR for internal rate of return Pmt for payment –Ucase/Lcase to convert to upper/lower case –Len for length of a string –Date for the system date –DateValue for the date corresponding to string argument We will use Pmt to compute the monthly payment –curMonPay = Pmt(sngRate, curNper, -curLoanAmt ) –Pmt(.08/12,60,-10000) = $256.03

19 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Creating a Monthly Payment Calculator Assume you wanted to determine the monthly payment necessary to pay off a loan at a given interest rate in some number of months Use PMT function PMT(rate, nper, pv) where –rate = monthly interest rate –nper = number of months –pv = negative value of loan amount

20 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors The Monthly Payment Form

21 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Compute Button Click event Dim curAmount As Currency, intMonths As Integer Dim sngRate As Single, curPayment As Currency curAmount = CCur(txtAmount) intMonths = CInt(txtMonths.Text) sngRate = CSng(txtRate.Text) curPayment = Pmt(sngRate, intMonths, -curAmount) txtPayment.Text = Format(curPayment, “Currency”) txtAmount.Text = Format(curAmount, “Currency”) txtRate.Text = Format(sngRate, “Percent”)

22 Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic Operators Constants Formatting Data VB Errors Visual Basic Errors Syntax errors: caused by incorrect grammar, vocabulary, or spelling. Also caused by using a keyword. Usually caught as you enter the statement. These are pointed out by VB and are usually easy to find and correct. Run time errors: errors not caught at entry but which involve an incorrect statement or bad data, e.g., dividing by zero. The presence of an error is detected by VB when you run the program, but you still have to find the source of the error. More difficult to correct than syntax errors. Logic errors: those that VB does not catch as being “wrong”, but which involve erroneous logic, say, only having a program include 11 months of data instead of 12. These are the hardest to find! Debugging is the art and science of finding errors. VB has debugging tools to be discussed later.


Download ppt "Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic."

Similar presentations


Ads by Google