variables hold information that may be manipulated, used to manipulate other information or...
DESCRIPTION
Things you can do with Variables Copy and store values entered by the user Perform arithmetic manipulation on values Test values to see if they meet a criteria Temporarily hold and manipulate the value of a control property Remember information for later use in the progamTRANSCRIPT
VariablesHold information that may be manipulated,
used to manipulate other information or remembered for later use
A storage location in memory (RAM)Holds information while the program is running
ContainsName (which cannot be changed)Data
Data type is declared and cannot be changed Value for the data can be changed during the program
Things you can do with VariablesCopy and store values entered by the userPerform arithmetic manipulation on valuesTest values to see if they meet a criteriaTemporarily hold and manipulate the value of
a control propertyRemember information for later use in the
progam
Declaring a VariableDeclaration statement creates the variable in
memoryIndicates the name to give the variable and
the type of information the variable will hold
DIM statementDim VariableName as DataType
ExampleDim intLength as IntegerDim intLength as Integer = 5
Dim Stands for dimension statementTells VB that you are creating a variable
intLength the name to give the variableAs Integer indicates that the variable will
hold integer numbers= 5 assigning a beginning value to the
variable
Variable Data TypesSee Handout
Boolean (bln)Byte (byt)Char (chr)Date (dtm)Decimal (dec)Double (dbl)Integer (int)Long (lng)
Object (default)(obj)
Short (shr)Single
(sng)String
(str)User Defined
Variable NamesFirst character must be a letter or underscore
Follow 3 character prefix styleAfter first character – you may use letters,
number, underscoreCannot contain spaces or periodsUsing multiple words, capitalize the first letter
of each word (example: intTestScore)Cannot use VB KeywordsShould be meaningful
Variable DeclarationsVariable should be declared first in the
procedure (style requirement)
Variable MUST be declared prior to the code where they are used
Declaring an initial value of the variable in the declaration statement is optionalWill default to a value of 0
Scope of VariablesScope – the part of the program where the
variable can be usedVariable cannot be used before it is declaredVariables declared within a procedure / event
handling method is only visible to statements within that method Called Local Variables
Can be declared at the beginning of the code window (General Declarations section) and be available to all methods Called Form Level Variables
Cannot have more than one variable with the same cope with the same name
Lifetime of VariablesLifetime – when the variable exists in
memoryA variables lifetime is the execution period of
the procedure/method that contains the variableWhen the procedure/method ends the local
variables are destroyed Memory is given back to the operating system
Performing Calculations with VariablesArithmetic Operators
^ Exponential* Multiplication/ Floating Point Division\ Integer DivisionMOD Modulus (remainder from division)+ Addition Subtraction& String Concatenation (putting them
together)
Mathematical Order of PrecedenceParenthesisExponentialMultiplication / DivisionInteger DivisionMODAddition / SubtractionString ConcatenationRelational Operators (< , > , >= , <= , <>)Logical Operators (AND, OR, NOT)
Examples5 * (4 + 3) – 15 Mod 2
7 * 4 / 2 - 6