Download presentation
Presentation is loading. Please wait.
Published byKristian Simpson Modified over 9 years ago
2
Variables Hold 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 Contains Name (which cannot be changed) Data Data type is declared and cannot be changed Value for the data can be changed during the program
3
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 progam
4
Declaring a Variable Declaration statement creates the variable in memory Indicates the name to give the variable and the type of information the variable will hold DIM statement Dim VariableName as DataType
5
Example Dim intLength as Integer Dim intLength as Integer = 5 Dim Stands for dimension statement Tells VB that you are creating a variable intLength the name to give the variable As Integer indicates that the variable will hold integer numbers = 5 assigning a beginning value to the variable
6
Variable Data Types See 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
7
Variable Names First character must be a letter or underscore Follow 3 character prefix style After first character – you may use letters, number, underscore Cannot contain spaces or periods Using multiple words, capitalize the first letter of each word (example: intTestScore) Cannot use VB Keywords Should be meaningful
8
Variable Declarations Variable 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 optional Will default to a value of 0
9
Scope of Variables Scope – the part of the program where the variable can be used Variable cannot be used before it is declared Variables 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
10
Lifetime of Variables Lifetime – when the variable exists in memory A variables lifetime is the execution period of the procedure/method that contains the variable When the procedure/method ends the local variables are destroyed Memory is given back to the operating system
11
Performing Calculations with Variables Arithmetic Operators ^ Exponential * Multiplication / Floating Point Division \ Integer Division MODModulus (remainder from division) +Addition Subtraction &String Concatenation (putting them together)
12
Mathematical Order of Precedence Parenthesis Exponential Multiplication / Division Integer Division MOD Addition / Subtraction String Concatenation Relational Operators (, >=, ) Logical Operators (AND, OR, NOT)
13
Examples 5 * (4 + 3) – 15 Mod 2 7 * 4 / 2 - 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.