340 likes | 362 Views
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.
E N D
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 • 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
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.
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
2 General Variable Types • Numeric • NonNumeric
2 Basic types of Numeric Variables • Integer • Decimal
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
A computer is really just a bunch of on/off switches These switches, bits, come in sets of 8, called bytes
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
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
Now that we have discussed integers we shall discuss decimal variables
Decimal types • Decimal types aren’t as simple as integer types, but still use bytes.
Decimal types • Single 38 or more places4 bytes • Double 308 or more places8 bytes • Currency (-922 trillion to 922 trillion)8 bytes
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
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
For instance… • To add 7 to the curNationalDebt, you may refer to it as 7@
Other Literals • Date - Date Literal#September 1, 1964# • String - String Literal“Enclosed in Quotes”
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
For this class, use variable prefixes: • Boolean blnYesOrNo • Byte bytAge • Currency curNationalDebt • Date dteBirthday • String strMyName • Variant vntUsesMemorycontinued
For this class, use variable prefixes: • Double dblKilometersToMars • Integer intStudentsAtMyHighSchool • Long lngUSPopulation • Object objMyStats • Single sngDistanceToMoon
How do you USE Variables… Now that we have discussed them…
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.
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.
The variables are empty. You may now fill them as needed. • Let bytAge=36or simply • bytAge=36
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)
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
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
Math Operation Priority • Parentheses • Exponentiation • Multiplication, Division, Integer Div, MOD • Addition/Subtraction • Left to Right • Do some math examples!