1 / 22

Chapter 3 : Variables, Assignment Statements, and Arithmetic

Chapter 3 : Variables, Assignment Statements, and Arithmetic. 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

ama
Download Presentation

Chapter 3 : Variables, Assignment Statements, and Arithmetic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. Learning Objectives Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  3. 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 Variables

  4. 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 - integer 255 - single 567.78 - long (integer) 35,455 - double 567.78129086 - Boolean True or False Data Types

  5. 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 Declaring Variables

  6. 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” The Option Explicit Statement

  7. 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 Event-driven Input Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  8. 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 Assignment Statements

  9. 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 datatype Using Functions

  10. 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 Using Assignment Statements for Calculations Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  11. () for grouping + for addition ^ for exponentiation - for subtraction - for negation * for multiplication / for division \ for integer division mod for modulus Arithmetic Operators Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  12. Operations within parentheses ( ) Exponentiation (^) Negation (-) Multiplication and division (*, /) Integer division (\) Modulo arithmetic (Mod) Addition and subtraction (+, -) String concatenation (&) Hierarchy of Operations

  13. 3 * (curSalary - curTaxes)^2 - curBonus/curMonths • 3 1 2 5 4 • Order • 1 Subtract Taxes from Salary • 2 Square the result • 3 Multiply this result by 3 • 4 Divide Bonus by Months • 5 Subtract result from first expression Arithmetic Example

  14. 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 Symbolic Constants Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  15. 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 Comments Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  16. 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”) Formatting Data

  17. 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 Print Form, Clearing Entries and Setting Focus Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  18. 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 Using Other Arithmetic Functions

  19. 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 Creating a Monthly Payment Calculator

  20. The Monthly Payment Form

  21. 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. 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. Visual Basic Errors

More Related