Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT.

Similar presentations


Presentation on theme: "Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT."— Presentation transcript:

1 Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT

2 Copyright © Don Kussee 1120-Ch3 #482 Data Types Strings –“Visual Basic is easy to learn and use.” –“Don” “X” “286 Main Street” “3” –“” ‘ The empty string Numbers (can be used for math functions) –286 –-308.455 –14259578374679.00974

3 Copyright © Don Kussee 1120-Ch3 #483 Constants Fixed at run time Cannot be changed Literal constant - the value itself Symbolic constant - pi = 3.1416

4 Copyright © Don Kussee 1120-Ch3 #484 Numeric values 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

5 Copyright © Don Kussee 1120-Ch3 #485 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

6 Copyright © Don Kussee 1120-Ch3 #486 Define & Declare Constants Const is a reserved word used to indicate this symbol name and it’s value Const SALES_TAX_RATE = 0.081 Const COMPANY_ADDRESS = “347 Main Street, Orem UT, 84058” Const DISK_ERR= “There is no disk in A:”

7 Copyright © Don Kussee 1120-Ch3 #487 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

8 Copyright © Don Kussee 1120-Ch3 #488 “Special” numbers to remember Bit - 1 or 0 - All the computer can use. Byte - 8 bits –256 different codes - 0 through 255 K is 1,000 Bytes (really 1024 Bytes) Meg is 1,000,000 Bytes (really 1,024,000) Gig is 1,000 Meg Bytes Tara is 1000 Gig Bytes

9 Copyright © Don Kussee 1120-Ch3 #489 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

10 Copyright © Don Kussee 1120-Ch3 #4810 Variables A named memory, used as temporary holder Current value is replaced forever by the new Define & Declare DIM Name as String –name –data type Initialization Name = “Don” –VB makes it “” (empty string ) or 0

11 Copyright © Don Kussee 1120-Ch3 #4811 Variable Names Name must be unique & should have meaning No Keywords 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 Alpha – no spaces, %, @,., #, !, $

12 Copyright © Don Kussee 1120-Ch3 #4812 Variable data types What kind of container Quart of water 4 books and 3 folders of papers 8 ski poles Piano Load of logs Family of 6 Quart of milk

13 Copyright © Don Kussee 1120-Ch3 #4813 Data types defined in VB String –Variable length –Fixed length Numbers –Integer, Long, Single, Double, Currency Others - Variant User defined

14 Copyright © Don Kussee 1120-Ch3 #4814 Number data types Whole numbers 37 -842 –Integer - small –Long - big Fractional numbers 37.7223 -842.01 –Single - Big –Double - Bigger –Currency Special Biggest –Date Special

15 Copyright © Don Kussee 1120-Ch3 #4815 Additional Data Types Byte 0 to 255 Boolean True or False Date Jan,1, 100; Dec.31,9999 Variant Date, Time, Universal Object (pointer) User defined - structures

16 Copyright © Don Kussee 1120-Ch3 #4816 Data type String Variable length - 2 Billion max –Dim FirstName as String –FirstName = “Don” FirstName= “Victoria” –Size changes automatically –Size 10 bytes + 1 byte per letter Fixed length - 65,400 max –Dim ZipCode as String * 5 –ZipCode = “48058”

17 Copyright © Don Kussee 1120-Ch3 #4817 Data Type Integer Whole number 89, -37, -32768, 32840 Memory set aside 2 Bytes Maximum Size +-32K (-32767 to +32768) Dim Age as Integer Age = 100 Fractional values are dropped

18 Copyright © Don Kussee 1120-Ch3 #4818 Data Type Long (Integer) Whole number 89, -37, -32768, 32840 Memory Set aside 4Bytes Maximum Size very big –-2,147,843,648 to 2,147,843,648 Dim BankBallance as Long ‘4 bytes BankBallance = 1000895 ‘ No comma’s

19 Copyright © Don Kussee 1120-Ch3 #4819 Data Type Single Fractional number 89.6 -37.0.00006 Maximum - Big number (-3.4E -45 to 3.4E 38 ) Memory Set aside 4 Bytes 7 Significant Digits –1.7894288.00017894288 17894288 Dim Wage as Single Wage = 5.90

20 Copyright © Don Kussee 1120-Ch3 #4820 Data Type Double Fractional number 89.6 -37.0.00006 –1.7894288.00017894288 17894288 Maximum - Very big (-1.9E 324 to 4.9E 308 ) Memory Set aside 8 Bytes 15 Significant Digits –-.0009787658654388955 9875.86445679987 Math takes six time

21 Copyright © Don Kussee 1120-Ch3 #4821 Data Type Currency Fractional number with 4 decimal places Maximum Very big (15.4 digits) Memory Set aside 8 Bytes Dim HouseLoan as Currency HouseLoan = 111211121112765.9999 Math takes six times

22 Copyright © Don Kussee 1120-Ch3 #4822 Decision rules for Standard Data Types Math to be done Size change? F String ! V String ! Currency? Currency ! No Yes No Yes

23 Copyright © Don Kussee 1120-Ch3 #4823 Decision rules for Math Data Types Decimals? > 7 Digits ? > 32 K ? Single Double Long Integer No Yes

24 Copyright © Don Kussee 1120-Ch3 #4824 Review of Standard Data Types Type Bytes Limit String 10 + character 2,000,000,000 Integer 2 +- 32 K Long 4 Big Single 4 7 digits Double 8 15 digits Currency 8 15.4 digits

25 Copyright © Don Kussee 1120-Ch3 #4825 Define, Declare and Initializing Variables Dim name as Data type Dim Name as String ‘ Empty string Name = “Don” Dim Age as Integer ‘ 0 value entered Age = 84 Dim WageRate as Single ‘ 0.0 value entered WageRate = 5.76

26 Copyright © Don Kussee 1120-Ch3 #4826 Variable Scope Depends on where and how declared –Scope How declared –Local - Dim, Const, Static, ReDim –Module - Dim, Private –Global - Global, Public Right mouse click will show properties of variable

27 Copyright © Don Kussee 1120-Ch3 #4827 Variable Scope & Life Procedure Declaration –Scope - local to procedure –Life - length of procedure Form Declaration –Scope - global to all procedures in form –Life - of application Global Declaration –Scope - global to application

28 Copyright © Don Kussee 1120-Ch3 #4828 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. Static Joe As Integer Dim Joe As Integer

29 Copyright © Don Kussee 1120-Ch3 #4829 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 = Auto-conversion will occur unless forced by programmer – But only in this language

30 Copyright © Don Kussee 1120-Ch3 #4830 String Concatenation & Adds a string to the end of another string String1 = “The big bad” String2 = “wolf.” String3 = String1 & String2 String3 now contains The big badwolf. String4 = String3

31 Copyright © Don Kussee 1120-Ch3 #4831 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 it’s hard to test.

32 Copyright © Don Kussee 1120-Ch3 #4832 Changes to the Property of a Controls 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

33 Copyright © Don Kussee 1120-Ch3 #4833 Variable (or Const) Scope What Variable can be accessed (read or changed) by a different domain Local - Dim within a Control_Event( ) –can only be accessed it’s domain Module -Dim in Form General Declarations –accessed by all the form’s controls domain-bad Global -Global in Form Declaration –anyone can access - generally bad

34 Copyright © Don Kussee 1120-Ch3 #4834 Variable Scope Local variables can not have the same name in the same procedure Local variables can have the same name in different procedures Local variables can have same name as Modules/Globals Computer looks local, then module, global All Forms are Global

35 Copyright © Don Kussee 1120-Ch3 #4835 Variable’s Life Local - while code runs –Except Static variables Module - while the form is in memory Global - while the Program runs Allows re-use of memory

36 Copyright © Don Kussee 1120-Ch3 #4836 Public Vs Private code Public changes all variables to Global Allows the code to be used by others Violates basic OOP concepts - very unusual

37 Copyright © Don Kussee 1120-Ch3 #4837 Methods Vs Properties Birds method = Flight,Properties =Red eyes 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

38 Copyright © Don Kussee 1120-Ch3 #4838 Code format Option Explicit Header Declarations Private Sub Object_Event ( ) –Header –Declaration –Procedure code End Sub

39 Copyright © Don Kussee 1120-Ch3 #4839 Data Type Fixed String Memory - Byte per space (here 26) Dim City as String * 26 Not used space is filled by NULL characters Overflow is truncated

40 Copyright © Don Kussee 1120-Ch3 #4840 Data Type Date 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

41 Copyright © Don Kussee 1120-Ch3 #4841 Data Type Boolean True or False Memory set aside 2 Bytes

42 Copyright © Don Kussee 1120-Ch3 #4842 Data Type Byte Whole positive number 0, 89, 255 Memory set aside 1 Bytes Maximum range 0 to 255 Dim Age As Byte Age =100

43 Copyright © Don Kussee 1120-Ch3 #4843 Data Type Variant 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

44 Copyright © Don Kussee 1120-Ch3 #4844 Data Type Object A pointer to an object –Do not use Let –Use Set –Use Set = NULL to free pointer before destroying an object.

45 Copyright © Don Kussee 1120-Ch3 #4845 Multiple Forms All the code is associated with the form and its controls and is stored in the.frm file Load event occurs when ever a form is displayed Form.Unload-code dumped from RAM Form.Load-code brought from disk to RAM Form.Hide-makes form disappear- in RAM Form.Show-Loads & Displays the form

46 Copyright © Don Kussee 1120-Ch3 #4846 More than one form in a project Add form to project Each form can change it’s or the other form’s properties Form1.Label1.BackColor = Form2.BackColor Move from one form to another with Hide & Show. Each Form will generate a new file

47 Copyright © Don Kussee 1120-Ch3 #4847 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

48 Copyright © Don Kussee 1120-Ch3 #4848 Computer Variable’s Terms Constant Variable Declare Initialize Integer Long Single Double String Concatenation Scope Variable lifetime Global scope Module scope Precision


Download ppt "Copyright © Don Kussee 1120-Ch3 #481 CNS 1120 Chapter 3 Data Types and Variables 1120-Ch3.PPT."

Similar presentations


Ads by Google