VB 2008 - Long Island University
Download
Report
Transcript VB 2008 - Long Island University
Creating Variables and Constants
Intro to Datatypes
Assignment Statement
A variable is a “friendly” way to refer to a piece of
memory in the computer
This memory location stores a value
The “DIM” command creates and associates this piece
of memory with a name in your program
Programmer specifies the “type”
Rules
Must start with an “alpha” character
Up to 256 alphanumeric and special characters
$ _ are special characters
Cannot be a reserved word
Reserved = for VB’s usage, such as “DIM”
NO SPACES!!!
A space is a delimiter
VB interprets the space as the end of something and beginning of
something else
Hungarian Notation
DIM intPasswordRetention as Integer
Pascal Notation
DIM PasswordRetention as Integer
Camel Notation
DIM passwordRetention as Integer
Creating a variable
Use the “DIM” statement
DIM intX as Integer
intX = 22
Same as
DIM intX as Integer = 22
//Assignment statement
What is an expression?
Something to be evaluated
Not an instruction per se
Examples:
3+5
dblRate * intNumberYearsMember
intAge > 21
The instruction which assigns a value to something
intAge = 10
boolAdult = intAge > 21
dinnerTip = subTotal * tipRate
The expression on the right of the equal sign is
evaluated, and then assigned to the variable on the left
of the equal sign
A=8
A=A+1
System.Console.WriteLine(A)
//displays 9
Declaration and assignment of a value to a name
Once assigned, the value cannot change
Same name rules as variable
Perhaps you might wish to put a “c” before the name to
indicate that it’s a constant
Const cintPasswordLimit as Integer = 3
Datatypes use a certain number of bytes in memory
Defines the possible range of values a specific type can
represent
Some types are “signed” while others are “unsigned”
One of the bits is used to indicate the positive or
negative value
This bit is not used for value, and halves the possible value
that can be held
Signed Short Integer uses 15 bits for value and 1 for sign
(-32768 thru +32767)
Unsigned Short Integer
(0 thru 65535)
Numeric
Integral (whole number) types
Signed
SByte
8 bit
-128 through 127
Short16 bit
-32,768 through 32,767
Integer
32 bit
Long
64 bit
-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
Unsigned
Byte
UShort
UInteger
ULong
8 bit
16 bit
32 bit
64 bit
Numeric
Non-integral (non whole numbers)
Decimal
79,228,162,514,264,337,593,543,950,335 through
79,228,162,514,264,337,593,543,950,335 (if no decimal places used)
7.9228162514264337593543950335E+28
Floating Point (larger ranges than decimal, but might have
rounding errors)
Single
Smallest values
-1.401298E-45 for negative values and 1.401298E-45
Largest values
3.4028235E+38
Double
Smallest values
4.94065645841246544E-324 for negative values and 4.94065645841246544E-324 for
positive values
Largest values
1.79769313486231570E+308
Integer data math is faster (performance) than using
non-integer types
Reference
http://msdn.microsoft.com/en-us/library/ae55hdtk.aspx
String
Group or “string” of characters
Can hold up to about 2 billion Unicode characters
Char
Holds a single Unicode character
Boolean
Value is either True or False
Used in decisions
In many cases a “non-zero” value equates to a True
Sometimes the data you get is not the datatype that
you need
You cannot manipulate or use it directly
Convert it to the type you DO need and can use
If you ask a user for taxrate they will typically use the
keyboard to type in the response
This is a bunch of letters (keystrokes) that merely
resemble numbers
If you can type it on the keyboard, or display it on the
monitor / printer, then it is character data
A long continuous group of characters is a character “string”
Accepting string input which should be numeric
Convert string data to numeric data
Input comes from:
Keyboard (command line)
Form’s text box
Text file
Presenting (outputting) to the user a number on the
screen
Convert the number to string / text data
Output goes to:
Screen (command line)
Form’s text box
Text file
Getting a numeric value
Valid characters which can be typed
All keyboard characters
Valid characters you can convert to numeric from string
0-9
Minus sign
Decimal point
Invalid characters which will not convert
Any other characters, including $ and comma
If you allow users to type in non-numeric characters, you must
strip away those characters before converting the remaining
characters to a numeric value usable by your program
Do not assume that the user will type in anything correctly or in
the way you expect
Validate the data!!!
Using the tax and taxrate example
When prompted for the taxrate we’ll assume the user wants 10%
We will also assume the expression tax = amount * taxrate and that
amount is 200
If the user types 10%
The “%” sign cannot be converted, we must strip it out
If the user types 10
Then tax would be 2000
The correct answer should be 20!
The user could’ve typed in .1 which is 10% or 10/100 (10 per 100)
How many users know this?
If you didn’t tell the user exactly how to enter the value, you’re guessing (and
hoping) as to what is entered
Your program should do the conversion, “behind the scenes”, for the user
tax = amount * taxrate / 100
Now if the user types “10” for taxrate the answer for tax would be “20”
Parenthesis
( )
Exponentiation
^
Positive / negative
+ Multiplication, division, integer division and modulus
* / \ MOD
Addition and subtraction
+-
Process of converting data from one datatype to another
In many cases, VB will do this “automatically” (Implicit Casts)
Widening
The receiving variable is “wider” and can hold the value being received
Dim a As Double = 6.5
Dim b As Integer = 6
Dim c As Integer = 10
Dim average As Double = (a + b + c) / 3
b and c are implicitly converted to Double values
Narrowing
The receiving variable is not “wide enough” to hold the value
A casting exception is thrown if receiving variable cannot hold the value
Shouldn’t use this unless you KNOW the receiving variable can hold the value
Dim grade As Integer = 93.75
Dim grade As Integer = txtSubtotal.text
Dim average As Integer = (a + b + c) / 3
‘Rounded to 94
‘may throw an exception
‘average is 8
Explicit Casts using code
CInt(x) or CDec(y) will convert the value to the type for usage
Explicit casts are performed before other operations in arithmetic
expressions