Presentation is loading. Please wait.

Presentation is loading. Please wait.

Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-1 Representing Data Constants and Variables chapter THREE.

Similar presentations


Presentation on theme: "Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-1 Representing Data Constants and Variables chapter THREE."— Presentation transcript:

1 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-1 Representing Data Constants and Variables chapter THREE

2 3-2 Representing Data Constants and Variables After studying this topic you should be able to: Differentiate between numeric and string data. Determine whether a data item should be a constant or variable. Code constants and variables in event procedures. Describe the characteristics and uses of standard data types. Explain scope and describe the domain of variables in event procedures and forms. Create projects that consist of several forms.

3 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-3 Data item Decisions required to specify a data item in VB 

4 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-4 Data item String ConstantVariable Decisions required to specify a data item in VB 

5 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-5 Data item StringNumeric ConstantVariableConstantVariable First decision: numeric or string Second decision: constant or variable Decisions required to specify a data item in VB 

6 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-6 String Values A collection of printable symbols. Synonym: text Example: “CNS 1200” “Visual Programming 1 – Visual Basic ” “It was a dark and stormy night” Must be printable ANSI characters See ANSI table p. 175

7 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-7 Numeric values Numbers that may be used in arithmetic calculations Examples: 13 14.6 -8.77 -0.023 001.800 -.023 ‘0 to left of the decimal not required 001.800 ‘leading 0’s ignored - trailing 0’s contain information, carried if possible 8.9E-6 = 0.0000089 1.34E+3 = 1340. Note: no commas or $ in the number

8 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-8 Classification of constants in VB  Numeric or string constant SymbolicLiteral

9 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-9 Constants Fixed at run time Cannot be changed Literal constant - the value itself Symbolic constant - PI = 3.1416

10 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-10 Symbolic Constant Use a symbol - SALESTAXRATE - defined once, used many times Name - 255 characters letters & numbers 1st character must be a letter Spaces not allowed, SALES_TAX_RATE Reserved words not allowed - problem All Caps is the convention

11 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-11 Defining & Declaring Constants Const is a reserved word used to declare an immutable value and its symbolic name Examples: Const SALES_TAX_RATE = 0.081 Const COMPANY_ADDRESS = “347 Main Street, Orem UT, 84058” Const DISK_ERR= “There is no disk in A:”

12 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-12 No Magic Numbers Only 0, 1, -1, “” allowed as literal constants Named constant easier to understand Constant reduces a the chance for typos Makes a change throughout the program easy and correct, especially when the same value used for two different purposes. TAX_RATE goes from 0.081 to 0.086 Const SHIPPLING_PER_TON =.081

13 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-13 Visual Basic Constants Shipped as a part of Visual Basic A large number related to various objects BackColor, ForeColor, … vbRed, vbYellow, vbBlue, vbGreen … Alignment vbLeftJustify, vbRightJustify, vbCenter

14 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-14 New Line Constant Used in many languages for a carriage return and a line feed CHR(13) = Carriage return CHR(10) = Line feed vbNewLine = CHR(13) + CHR(10)

15 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-15 Which are Valid Constant Definitions? In-class exercise 3.2

16 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-16 Variables A named memory location used as temporary holder Current value is replaced forever by the new Declaration DIM Name as String Variable name Variable data type Initialization Name = “The Wolverine” VB provides default values Numeric variables 0 String variables “” (empty string )

17 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-17 Variable Names Name must be unique & should be meaningful Keywords may not be used as variable names Case Insensitive - but matches the Dim Dim is the keyword used to tell VB to Give it a name - to be used later in the program Set aside an amount of memory Name 255 characters, 1st letter alphabetic no spaces or special characters %, @,., #, !, $

18 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-18 Which are Valid Variable Names? In-class exercise 3.3

19 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-19 Data Types Defined in VB String Variable length Fixed length Numbers Integer, Long, Single, Double, Currency Others including Variant Avoid Variant in this course User defined

20 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-20 Data Representation Strings “Visual Basic is easy to learn and use.” “Jones” “X” “286 Main Street” “3” “” ‘ The empty string Numbers (can be used for arithmetic) 286 -308.455 14259578374679.00974

21 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-21 Numeric data types Whole numbers 37 -842 Integer - small Long - big Fractional numbers 37.7223 -842.01 Single - Big Double - Bigger Currency - Bigger without rounding errors

22 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-22 String Data Type Variable length -- 2 Billion max Example: Dim FirstName as String FirstName = “Christopher” FirstName= “Victoria” Size changes automatically Memory Size: 10 bytes + 1 byte per character Fixed length -- 65,400 max Example: Dim ZipCode as String * 5 ZipCode = “48058” Memory Size: 1 byte per character

23 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-23 Fixed String Data Type Required for Arrays and Record (Structure) Memory - Byte per space (here 26) Dim City as String * 26 Unused space is filled by NULL characters Overflow is truncated

24 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-24 String Concatenation & Appends one string to the end of another string Example: String1 = “The big bad” String2 = “wolf.” String3 = String1 & String2 String3 now contains The big badwolf.

25 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-25 Integer Data Type Whole number 89, -37, -32768, ?32840? Memory set aside 2 Bytes Maximum Size +-32K (-32768 to +32767) Example: Dim Age as Integer Age = 100 Fractional values are dropped

26 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-26 Long Data Type Whole number 89, -37, -32768, 32840 Memory Set aside 4Bytes Maximum Size very big -2,147,843,648 to 2,147,843,647 Example: Dim BankBalance as Long ‘4 bytes BankBalance = 1000895 ‘ No comma’s

27 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-27 Single Data Type Fractional number 89.6 -37.0.00006 Maximum - Big number (+- 1.4E -45 to 3.4E 38 ) Memory Set aside 4 Bytes 7 Significant Digits 1.7894288.00017894288 17894288 Example: Dim Wage as Single Wage = 5.90

28 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-28 Double Data Type Fractional number 89.6 -37.0.00006 1.7894288.00017894288 17894288 Maximum - Very big (+- 4.9 324 to 1.8E 308 ) Memory Set aside 8 Bytes 15 Significant Digits -.0009787658654388955 9875.86445679987

29 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-29 Currency Data Type Fractional number with 4 decimal places Maximum Very big (15.4 digits) Memory Set aside 8 Bytes Example: Dim HouseLoan as Currency HouseLoan = 111211121112765.9999 No rounding errors for money calculations

30 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-30 Decision rules for Standard Data Types Math to be done Size change? F String ! V String ! Currency? Currency ! No Yes No Yes

31 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-31 Decision rules for Math Data Types Decimals? > 7 Digits ? > 32 K ? Single Double Long Integer No Yes

32 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-32 Review of Standard Data Types Type Bytes Limit String 10 + character 2,000,000,000 Integer 2 +- 32 K Long 4 +- 2 Billion Single 4 7 digits Double 8 15 digits Currency 8 15.4 digits

33 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-33 Defining, Declaring and Initializing Variables Dim VariableName as DataType Examples: Dim Name as String ‘ Empty string Name = “Jones” Dim Age as Integer ‘ 0 value entered Age = 84 Dim WageRate as Single ‘ 0.0 value entered WageRate = 5.76

34 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-34 Variable Lifetime Local level variables Created when Function/Sub called Die when Function/Sub is exited Form level variables Created when Form is Loaded into memory Die when Form is un-Loaded from memory Global variables Created when program starts Die when program terminates

35 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-35 Variable Lifetime Local - while code runs Except Static variables Module - while the Form runs Global - while the Program runs Allows re-use of memory

36 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-36 Variable Scope Depends on where and how declared Scope How Declared Local Dim, Static, ReDim Module Dim, Private Global Global, Public Right mouse click will show properties of variable

37 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-37 Local variables  VariableValue MyName“John Doe” VariableValue MyName“ ”

38 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-38 VariableValue MyName“John Doe” VariableValue Module-level variable 

39 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-39 VariableValue MyName“John Doe” VariableValue Module-level variable 

40 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-40 Local variable hides module-level variable with same name  VariableValue MyName“John Doe” YourName“Mary Smith” VariableValue YourName“ ”

41 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-41 VariableValue OurName“John Doe” VariableValue A global variable 

42 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-42 The three levels of variable scope  A local variable is declared in an event procedure using a Dim statement. It can be accessed only in the event procedure in which it is declared.

43 3-43 User interface Code The three levels of variable scope  Form frmTestA A module-level variable is declared in the general declarations section containing the Dim statement. It can be accessed by any event procedure on the same form as the general declarations statement.

44 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-44 Form frmTestA Code module modTest Project User interface Code Global Variables are declared in the general declarations section of a code module using the Public statement, and can be accessed by the code in any event procedure anywhere in the project.

45 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-45 Static vs Dim Initializes variable the first time only, then the value is maintained for the remainder of the life of the program. Reuse of the variable (by entering the procedure), does not reinitialize the variable. Scope is unchanged. Example: Static Joe As Integer Dim Joe As Integer

46 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-46 Assignment operator = Target = Source ‘ required direction 3 = A if A = 7 and B = 2 then after A = B what is contained in both A and B ??? Target must be a variable Right side of equation is completely evaluated, then transferred over = Implicit type conversion will occur unless explicit conversion by programmer

47 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-47 Type Mismatch error Trying to put a golf club in a gallon jug. Dim x as Integer x = “14 inches” ‘ Error x = 14.99 ‘No Error auto truncation to 14 Run time error - Depends on values when the program is running so can’t be tested.

48 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-48 Changes to the Property of a Control Label1.Caption = “New Caption” Label2.Caption = Label1.Caption Most properties can be Set or Read Most properties can change Some (Name, Tag,…) can not be changed Some are “Read only at run time” and can not be changed

49 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-49 Public vs Private code Public changes all variables to Global Allows the code to be used by others Violates good programming practices Avoid Globals

50 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-50 Methods vs Properties Object.Method vs. Object.Property Properties Store a Numeric or string value Can be used in an assignment statement Methods Cannot be used in assignment statements Must be on a line by itself

51 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-51 Additional Data Types Byte 0 to 255 Boolean True or False Date Jan,1, 100; Dec.31,9999 Variant Any numeric or string value Object (reference) User defined – Structures (records), Classes

52 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-52 Byte Data Type Whole positive number 0, 89, 255 Memory set aside 1 Byte Maximum range 0 to 255 Dim Age As Byte Age =100

53 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-53 Boolean Data Type True or False Memory set aside 2 Bytes

54 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-54 Date Data Type Contains both date and time information Date.Time Memory set aside 8 Bytes Date Jan.1, 100 to Dec.31, 9999 Time 00:00:00 to 23:59:59 Noon =.5000, Midnight or 00:00:00 = 0

55 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-55 Variant Data Type Default data type Memory set aside 17 Bytes Will hold any data type (Byte, Boolean, Date, Double, Integer, Long, Single, String, Objects) and Empty, Error, Nothing, Null type = VarTyp (variable name) ‘defines type of variable contained

56 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-56 Object Data Type A reference to an object Do not use Let Use Set

57 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-57 Multiple Forms All the code is associated with the form and its controls and is stored in the.frm file Load event occurs whenever a form is displayed Form.Load -code is placed in RAM Form.Unload -code is removed from RAM Form.Show - Loads & Displays the form Form.Hide -Removes from display (but still in RAM)

58 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-58 More than one form in a project Add form to project Each form’s controls have access to themselves or the controls on other forms Form1.Label1.BackColor = Form2.BackColor Cycle from one form to another with Hide & Show properties.

59 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-59 Adding & Deleting a Form Adding a new form to a project Use New Form Icon or Project New Form Project Explorer shows the additional form Deleting a form from a project Highlight in Project Explorer window Project Remove Form New forms generate a new file when saved

60 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-60 Typical Contents of.frm file VERSION 5.00 Begin VB.Form Form1 Caption = "Assign01-Gross Pay Calculator" ClientHeight = 3465 ClientLeft = 1545 ClientTop = 1965 ClientWidth = 3600 BeginProperty Font Name = "MS Sans Serif" Size = 8.25 Charset = 0 Weight = 700 Underline = 0 'False …

61 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-61 Contents of.vbp file Type=Exe Form=Assign2.FRM Reference=*\G{00025E04-0000-0000-C000- 000000000046}#3.5#0#..\..\..\..\..\..\..\PROGRAM FILES\COMMON FILES\MICROSOFT SHARED\DAO\dao2535.tlb#Microsoft DAO 2.5/3.0 Compatibility Library IconForm="Form1" Startup="Form1" ExeName32="Assign2.exe" Command32="" Name="Prob2" HelpContextID="0"

62 Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-62 Lab: Data Types Complete Exercise 3.16


Download ppt "Irwin/McGraw-Hill © The McGraw-Hill Companies, Inc., 1999 3-1 Representing Data Constants and Variables chapter THREE."

Similar presentations


Ads by Google