Tutorial 6 Slides

advertisement
Tutorial 6
Introducing Variables, Memory
Concepts & Arithmetic
Variables






Holds data
Controls for Design Programming, Variables
for Code Programming
Text Property  Data  Variable
Manipulate data w/o showing to users
Store data w/o adding or using controls
Hold numbers, data & time, text, and etc.
Continue 

One type of variable can take the same
type of data. E.g.
 intCount
= 15 (OK)
 intCount = “Pat” (not OK)
 intCount = “15” (not OK)

Must be declared. E.g.
 Dim
intCount As Integer
 Dim sngPrice As Single
Continue 
Steps of Using Variables
Declare
 Input: Assign a value by reading in from a
control or other source
 Process: Use or manipulate
 Output: Put the value to a control or other
object

Example 1:
Dim intTotal As Integer
intTotal = Val(txtTotal.Text)
intTotal = intTotal + 10
txtTotal.Text = Str(intTotal)
Example 2:
Dim intTotal As Integer
Dim intExtra As Integer
intTotal = Val(txtTotal.Text)
intExtra = 10
intTotal = intTotal + intExtra
txtTotal.Text = Str(intTotal)
Example 3:
Dim intTotal As Integer
Dim intExtra As Integer = 10
intTotal = Val(txtTotal.Text)
intTotal = intTotal + intExtra
txtTotal.Text = Str(intTotal)
Data Types of Variables
Data Type
Memory Allocation
Value Range
Boolean
2 bytes
True or False.
Char
2 bytes
0 through 65535 (unsigned).
Date
8 bytes
0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999.
String
Depends on platform
Short
2 bytes
-32,768 through 32,767.
Integer
4 bytes
-2,147,483,648 through 2,147,483,647.
Long
8 bytes
-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.
Single
4 bytes
-3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through
3.4028235E+38 for positive values.
Double
8 bytes
-1.79769313486231570E+308 through
-4.94065645841246544E-324 for negative values; 4.94065645841246544E-324
through 1.79769313486231570E+308 for positive values.
Decimal
16 bytes
0 through +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
0 through +/-7.9228162514264337593543950335 with 28 places to the right of the
decimal; smallest nonzero number is
+/-0.0000000000000000000000000001 (+/-1E-28).
0 to approximately 2 billion Unicode characters.
Integer



Stored as 32-bit (4-byte) integers ranging in
value from -2,147,483,648 through
2,147,483,647.
Provides optimal performance on a 32-bit
processor, as the smaller integral types are
slower to load and store from and to memory.
You can convert the Integer data type to Long,
Single, Double, or Decimal without
encountering a System.OverflowException
error.
Single


Stored as IEEE 32-bit (4-byte) single-precision
floating-point numbers ranging in value from 3.4028235E+38 through -1.401298E-45 for
negative values and from 1.401298E-45 through
3.4028235E+38 for positive values. Singleprecision numbers store an approximation of a
real number.
Can be converted to the Double or Decimal
data type without encountering a
System.OverflowException error.
String





Stored as sequences of 16-bit (2-byte) numbers ranging
in value from 0 through 65535
Each number represents a single Unicode character. A
string can contain up to approximately 2 billion (2 ^ 31)
Unicode characters
The first 128 code points (0–127) of Unicode correspond
to the letters and symbols on a standard U.S. keyboard
The second 128 code points (128–255) represent
special characters, such as Latin-based alphabet letters,
accents, currency symbols, and fractions
The remaining code points are used for a wide variety of
symbols
Prefixes for Variables
Data Type
Prefix
Data Type
Prefix
Boolean
bln
Integer
int
Byte
byt
Long
lng
Currency
cur
Short
sht
Date(time)
dtm
Single
sng
Double
dbl
String
str
Arithmetic Operators
Order
Operator
Mathematical Operation
Example
1
()
Parentheses
2
^
Exponentiation
CubicSpace = Length ^ 3
8=2^3
3
-
Negation
- Num1
4
*
or
/
Multiplication
Division
Total_Sales = Unit_Price * Count
ClassAve = TotalScores /
NumOfStudents
5
\
Integer Division (DIV). The
whole number portion of
the answer in a division.
The decimal position is
dropped.
CarsCanFitIn = AreaOfLot \ CarDim
10 = 215 \ 20
15 is leftover
Continue 
Order
Operator
Mathematical Operation
Example
6
MOD
Integer Remainder Division.
The whole number portion of
a remainder in a division.
If the divider is greater than
the number it is dividing,
then the number become the
remainder of the MOD
division.
BoothSpace = 3 MOD 10
3 = 3 MOD 10
7
+
or
-
Addition
Subtraction
Total = Num1 + Num2
Profit = Sales - Expenses
&
String concatenation
FullName$ = First$ & Last$
Using Debugger

Demo
Download