Download presentation
Presentation is loading. Please wait.
Published byJuan Barbero Modified over 5 years ago
1
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019
2
Topics to cover Arithmetic Operators Numeric Variables
Naming Conventions Declaring Variables Prepared By: Deborah 7/9/2019
3
Arithmetic Operators * (Multiplication) / (Division) + (Addition)
- (Subtraction) ^ (Exponential) Prepared By: Deborah 7/9/2019
4
Expression Evaluation
In an expression in VB if No parentheses are used the equation is evaluated from left to right. The order of operations is Exponential Multiplication & Division Addition & Subtraction Prepared By: Deborah 7/9/2019
5
Calculations Additions + A + B Substraction - A – B Multiplication *
Division / A / B Exponentiation ^ A ^ B Left to Right Prepared By: Deborah 7/9/2019
6
Evaluate these equations
Assume x = 2 y = 4 z = 3 X + Y ^ 2 8 / Y / X X *(X + 1) X * X + 1 Y ^ X + Z * 2 Y ^ (X + Z) * 2 (Y ^ X) + Z * 2 ((Y ^ X) + Z) * 2 Prepared By: Deborah 7/9/2019
7
ANSWERS 18 1 6 5 22 4 To Power of 5; multiply by 2 (2048) 38
Prepared By: Deborah 7/9/2019
8
VB Data Types Boolean Byte Currency Date/time Double Integer Long
Single String Variant (default) 1) String or String Literal enclosed in quotes (literal) can be any characters, symbols, or numbers example: strLastName = "Satzinger" strFirstName = "John" strFullName = strFirstName & strLastName 2) Integer a whole number (no decimal place) a numeric value, stored differently no quotes example: intPageCounter = intAge = 29 3) Currency a number with decimal places used in calculations, not limited to "money" (no dollar sign) examples: curSalesAmount = curSalesTax = curDiscountRate = .045 Prepared By: Deborah 7/9/2019
9
Naming Conventions bln Boolean cur Currency dbl Double dtm Date int
Integer lng Long sng Single str String vnt Variant (default) Just like objects have prefixes variable also have prefixes that we will use in this class. These will help you to identify different variable within the code and save you time during the debugging phase. Not using the correct prefix for objects and variables will costs you points in your home work and on test. Prepared By: Deborah 7/9/2019
10
VB Numbers Numeric types in Visual Basic Integer Long Currency Single Double Variant 1 3 $3.00 Prepared By: Deborah 7/9/2019
11
VB Data Types Memory Reqmts.
Prepared By: Deborah 7/9/2019
12
Naming Rules and Conventions
Identifier Type Prefix Identifier (name) 1 to 255 characters Can use Letters, digits, underscores Cannot use spaces, or reserved words (Print, Name, Value, Sub, etc.) strSocialSecurityNumber curPayRate intQuanity lngPopulation curTAX_RATE (constant) Naming convention requires: 1) Identifier is meaningful -- A name that indicates the purpose 2) Begins with lowercase prefix specifying data type Starting with VB 6.0 the lowercase prefixes are now three letters as per the Microsoft MSDN Library e.g., str for string int for integer cur for currency For variables, capitalize each word of name after prefix e.g., curHoursWorked For constants, use full uppercase and underscore e.g., curMINIMUM_WAGE Prepared By: Deborah 7/9/2019
13
When To Use a Numeric Data Type?
Only if data will be used in a calculation, otherwise use String Examples: Course Number? Social Security Number? Room Number? Age? Salary Course Hours? Pay rate Prepared By: Deborah 7/9/2019
14
Declaring Variables Implicit vs. Explicit
Option Explicit statement required in this class!!!!! Dim statement (referring to Dimension or size) This requires explicit declaration of variables. You must declare all variables used in the program! The Auto Fill In feature help you write your dimension statements. If you find the Auto Fill In feature annoying it can be turn off by: Tools/Options/Editor Deselect the Auto List Members Dim strCustomerName as String Dim intTotalSold as Integer Dim curDailySales as Currency Prepared By: Deborah 7/9/2019
15
Where to declare identifiers?
Identifier-another name for variable. (A variable is a data element that is part of a data file or used in calculations with the code section of your project). Variables can be declared in the programs general declaration area or in procedures. Variable declared in the GD area can be used in all procedures within the Form Variables declared in Sub Procedures can only be used within that procedure. (Scope) Scope or Focus Prepared By: Deborah 7/9/2019
16
Scope of Variables Visibility of Variable
Can it be seen or used in this location? Global - used anywhere in project Public - used to declare global variables Module-Level Variables – used in all procedures of a form mstrFirstName (module-level string) Local – used only in the current procedure Static – used only in a procedure but retains its value while form is executing Need to control when and where a variable can be used or its value changed Global Variable (In a later chapter) Public Variable can be "seen" or used anywhere in the project (multiple forms)--Public rather than Dim Module-Level Variable or sometimes-called Form-Level Variable, are variable that can be "seen" or used anywhere in the code attached to a form. They are declared in the General Declarations of the Form. Local Variable—can be "seen" or used only in the procedure where it is declared and is Declared in the Procedure where it is used You can indicate scope in the variable name for module variable by add a m to the variable name Prepared By: Deborah 7/9/2019
17
curTaxes = curTaxRate * curPay
Guidelines Declare identifiers for Numeric values that will be needed in your program If you need to use a literal value declare a constant in the GD In the GD area declare constant values like this Const sngTaxRate as Single = .76 Constant variable values may not be reassigned at run time. They hold the original value while the form is active. We use variables to make our programs more functional. Using literals and numeric assignment of literal values within your programs causes the program to be hard to maintain. I call these magic numbers Numeric Literals in programs require continual maintenance Always declare a variable for your data and use that variable name in your calculation, and assignment statements. This will add to the lines of code needed but it will also make you program more functional OK to assign numeric value to Label Caption lblTotal.Caption = curSalesAmount + curSalesTax HOWEVER: if the value will be used later for additional calculations it should be placed in a variable not a property. Basic Code Example curTaxes = curTaxRate * curPay * Do not use numeric literals in calculations Prepared By: Deborah 7/9/2019
18
Calculations Calculations can be preformed with variables, constants and with the properties of certain objects. The Text property of a TextBox is actually a string. If you must use the data entered into the TextBox in a calculation you must use the Val Function to convert the text to a numeric value. Prepared By: Deborah 7/9/2019
19
Val Function curPrice = Val(txtPrice.Text)
Use Val function to convert String data to numeric data curSalesAmount=Val(txtSalesAmount.Text) Some programs declare all variables as string and use the val function to do calculations on numeric data. The format is curPrice = Val(txtPrice.Text) Use for Validating data - (An additional process for validation will be used when the IF statement is introduced—“Is Numeric”) if user types in number to text box, fine if user types anything else, Val returns 0 this prevents error conditions (like the non-numeric data error) Prepared By: Deborah 7/9/2019
20
Counting & Accumulation Sums
Declare Module-Level variables in the General Declaration section mcurScoreSum mintTotalCount In the TextBox procedure the basic code will look like this: We use counter to count the number of time that a sequence of code has been executed. We use accumulator variables to accumulate the value of data read into the program Usually counters and accumulators are declared as modular or static variables Prepared By: Deborah 7/9/2019
21
Using The Format Function
To format means to control the way output will look, either on the screen or on a report. VB 6 has four new formatting functions FormatCurrency FormatNumbers FormatPercent FormatDateTime Prepared By: Deborah 7/9/2019
22
Formatting Data See Handout Prepared By: Deborah 7/9/2019
23
Format this numeric data
Your rate of pay (10.50) to 2 decimal places in curPayRate The sales tax rate (.75) as a percentage in curSalesTax With one decimal With two decimals The dtmTodaysDate to show a long date Prepared By: Deborah 7/9/2019
24
Answers FormatCurrency(curPayRate) $10.50 FormatPercent(curSalesTax)
FormatDateTime(dtmToday, vbShortDate) $10.50 75% 75.0% 75.00% 9/7/00 Prepared By: Deborah 7/9/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.