Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants,

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants,"— Presentation transcript:

1 McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants, & Calculations

2 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 2 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 3 McGraw-Hill/Irwin Data—Variables & Constants Data Types –Boolean, Byte, Currency –Date, Double, Integer, Long –Single, String, Variant intMaximumNumber = 100 intMaximumNumber 100

4 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 4 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 5 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 6 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 7 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 8 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 9 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 10 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 11 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 12 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 13 McGraw-Hill/Irwin 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 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 14 McGraw-Hill/Irwin Format Function Examples lblTot.Caption = FormatCurrency(curTotal) lblDate.Caption = FormatDateTime(intBirth) lblQuantity.Caption = FormatNumber(intDone) lblPercentDone.Caption = FormatPercent(txtDaysWork/txtTotalDays)

15 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 15 McGraw-Hill/Irwin Counting and Accumulating Sums Counting: intCount = intCount + 1 Accumulating a sum: mintTotal = mintTotal + intNew Averaging: msngAverage = mintTotalScore / intCount

16 Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 16 McGraw-Hill/Irwin Calculation Program Example summary information


Download ppt "McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants,"

Similar presentations


Ads by Google