1 / 34

VB Variables and Data

VB Variables and Data. 091200. About this Presentation. Do not feel you have to memorize all the technical details yet. Know it well enough that you know you need to look it up when needed. Know the basics. Types of data. Variable Constant Literal. Variables.

Download Presentation

VB Variables and Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB Variables and Data 091200

  2. About this Presentation • Do not feel you have to memorize all the technical details yet. • Know it well enough that you know you need to look it up when needed. • Know the basics.

  3. Types of data • Variable • Constant • Literal

  4. Variables • A variable is like a box in the computer memory for holding values. • Depending on the memory requirements of the variable, we may specify a large or small box.Dim intAge as ByteDim curNationalDebt as Currency

  5. A constant is a variable that doesn’t vary! • In other words, you may specify a value, such as PI, that you never want to change in your program.

  6. Literal • A literal is a number that does not take up a variable position. • You simply refer to that number, such as 7 in this example:intHumanYears = intDogAge * 7

  7. 2 General Variable Types • Numeric • NonNumeric

  8. 2 Basic types of Numeric Variables • Integer • Decimal

  9. Integer types • Byte (0-255)1 byte: 256 values • Integer (-32,768 to 32,767)2 bytes: 256^2 values • Long (-2,147,483,648 to 2, 147,483,647)4 bytes: 256^4 values

  10. A relevant tangent…

  11. A computer is really just a bunch of on/off switches These switches, bits, come in sets of 8, called bytes

  12. A single byte is 8 on/off switches containing 256 possible values: • 0 1 • 0 2 • 0 4 • 0 8 • 0 16 • 0 32 • 0 64 • 0 128

  13. The zeros represent OFF, the ones ON. This number is 49 • 1 1 • 0 2 • 0 4 • 0 8 • 1 16 • 1 32 • 0 64 • 0 128

  14. Now that we have discussed integers we shall discuss decimal variables

  15. Decimal types • Decimal types aren’t as simple as integer types, but still use bytes.

  16. Decimal types • Single 38 or more places4 bytes • Double 308 or more places8 bytes • Currency (-922 trillion to 922 trillion)8 bytes

  17. Scientific Notation • 1.44E+5 = a number with the decimal shifted 5 places to the right:144,000 • 1.44E-5 = a number preceded by 5 zeros0.0000144

  18. Forcing a Literal • Occasionally you may want to force a literal number to be more precise so the computer allocates enough memory for a calculated result. • & Long • ! Single • # Double • @ Currency

  19. For instance… • To add 7 to the curNationalDebt, you may refer to it as 7@

  20. Other Literals • Date - Date Literal#September 1, 1964# • String - String Literal“Enclosed in Quotes”

  21. NonNumeric Data Types

  22. Nonnumeric Data • Strings Textlength+10 bytes • Boolean True/False 2 bytes used • Date Various date formats100AD to 9999AD • Variant All types of data Numeric data uses 16 bytesString data uses Length + 22 bytes • Object The reference to an object

  23. For this class, use variable prefixes: • Boolean blnYesOrNo • Byte bytAge • Currency curNationalDebt • Date dteBirthday • String strMyName • Variant vntUsesMemorycontinued

  24. For this class, use variable prefixes: • Double dblKilometersToMars • Integer intStudentsAtMyHighSchool • Long lngUSPopulation • Object objMyStats • Single sngDistanceToMoon

  25. How do you USE Variables… Now that we have discussed them…

  26. Remember all of this next part…

  27. First, lets tell the computer to help us. • Add Option Explicit To the first line of your code, in the general declarations part of your code. • This catches misspelled variables that could cause you grief • It minimizes the chances of you using one name for two different intended variables.

  28. Make your variable boxes, and make them the right sizes. • Dim bytAge as Byte • Dim datMyDOB as Date • Dim strMyName as StringThis is also done in general declarations, OR in a subroutine. If done in a subroutine it is used locally – in that subroutine only.

  29. The variables are empty. You may now fill them as needed. • Let bytAge=36or simply • bytAge=36

  30. And you can do math… • ^ Exponentiation bytYourAge=2^2 • * Multiplication bytAge=9*4 • / Division byYourAge=24/3 • + Addition bytAge=30+6 • - Subtraction bytAge=40-4letbytAge=bytYourAge * 4.5 • NOTE: Your book has errors in this (Table 5.5)

  31. And more… • MOD Modulus intMyRemainder = 11 MOD 3 MOD only works with Integers. • \ Whole number divisionintMyWholePart = 11\3 intMyRemainder = 2, and intMyWholePart = 3 Because 11/3 is 3 remainder 2

  32. Concatenation • Concatenation means joining strings • We could use + or & but we won’t use the + • We will use "&" because it is easier to understand and distinguish from math

  33. Math Operation Priority • Parentheses • Exponentiation • Multiplication, Division, Integer Div, MOD • Addition/Subtraction • Left to Right • Do some math examples!

More Related