Download presentation
Presentation is loading. Please wait.
Published byMadeline Banks Modified over 9 years ago
1
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San Diego
2
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Ch. 3:Variables, Constants, & Calculations Data—Variables, Constants, & Controls Data Types Naming Conventions Variable Scope Calculations Val function Arithmetic Operations Formatting Data Programming Hints—Counting/Summing
3
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Data—Variables & Constants Data Types –Boolean, Byte, Currency –Date, Double, Integer, Long –Single, String, Variant intMaximumNumber = 100 intMaximumNumber 100
4
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Declaring Variables & Constants Variables: Dim variable-name As data-type Constants: Const constant-name As data-type = value Most important: Force varibles to be explicitly declared: Select Tools, Options, Editor tab, and click the “Require Variable Declaration” check box.
5
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Naming Variables & Constants Naming rules keep track of variable types Naming rules help you remember which are constants and which are not They avoid data conversion Default data type is Variant You can detect misspellings by typing variables in lowercase
6
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Naming Variables All lowercase variables are converted to their “dimmed” spelling. Attach prefix to the variable name: bln-Boolean, cur-currency, int-integer, sng-single precision, str-string Examples: strSocialSecNum, intQuantity, intHoursWorked, curPayRate
7
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Constants—Named & Intrinsic Using named constants follows conventional programming practice Use named constants in lieu of “magic numbers” –increases documentation, –decreases likelihood of errors –facilitates global value changes –eases program maintenance
8
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill More about Constants Examples of declaration: Const strRptName As string = “October Report” Const curTaxRate As Single = 0.075 Place constant declarations in lowest "covering level" you can.
9
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Intrinsic Constants Intrinsic constants are system-defined constants They use a two-character prefix to indicate the source –vb means Visual Basic –db means Data Access Objects –xl means Excel
10
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Scope of Variables Scope is the availability of a variable –Global: can always be used or seen across the entire project –Module: accessible by all procedures in the form –Local: accessible only from single procedure in which it is declared –Static: local variables that are “remembered” across invocations of a routine but not outside it
11
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Scope of Variables (continued) Code module-level declarations in General Declarations section of a form. Code Local and Static variables in subprocedures. Variable's lifetime is the period of time that the variable exists.
12
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Calculations & the Val Function Standard Operators: (), ^, *, /, +, - Operator precedence determines how an equation is evaluated Example: curTotal = curTotal + curLatest Val function converts Text to value; reduces errors for blank entries in text box Counting occurrences: intSum = intSum + 1
13
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Formatting Data Alter the displayed form of a number or string by formatting it Special format functions exist to speed the process including FormatPercent Form of a typical format function: FormatCurrency(NumericExpressionToFormat) FormatCurrency(curTotalDue)
14
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Format Function Examples lblTot.Caption = FormatCurrency(curTotal) lblDate.Caption = FormatDateTime(intBirth) lblQuantity.Caption = FormatNumber(intDone) lblPercentDone.Caption = FormatPercent(txtDaysWork/txtTotalDays)
15
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Counting and Accumulating Sums Counting: intCount = intCount + 1 Accumulating a sum: mintTotal = mintTotal + intNew Averaging: msngAverage = mintTotalScore / intCount
16
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Calculation Program Example summary information
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.