0 index
1 Variable Description
2 Principal Uses
3 Type of Variables
4 Variable Declaration Modes
5 Explicit Declaration
6 Advantages of using variables
7 Scope
8 Description
9 Law Enforcement
10 General (form) Level Variables
11 Object Level Variables
12 Which should you use?
13 Type Conversion Functions
14 Simple Dialog Boxes
15 Inputbox
16 If - Then - EndIf
17 Block for of IF Else EndIF
18 Example:

outline
created using slideshow.cgi by Andy Harris















CSCI N331 Visual Basic: n331/vb04VariablesandScope
1. Variable Description
  • A container for information (data)
  • A name that refers to a chunk of computer memory
  • Variables have value
  • Can be used in statements rather than a value
  • Value can change



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
2. Principal Uses
  • Storing information gotten from the user, environment or another source
  • Works with information stored in object properties
  • Can manipulate information (info garage)



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
3. Type of Variables
  • Computer stores different types of information in different ways
  • Examples:
    Text - fixed or dynamic length
    Integers - integers or long integers
    Real numbers - single and double precision
    Currency - 8 digits plus four decimal places
    Variant - computer takes its best guess (risky)



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
4. Variable Declaration Modes
  • Implicit - simply by using the variable you declare its existence
  • This is used by older forms of Visual Basic
  • Used type declaration characters ($ % & #, etc.)
  • There is no way to check for spelling of variable names - common errors



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
5. Explicit Declaration
  • Each variable is declared in a DIM statement, using a variable name and the type
  • Example Syntax:
    - Dim UserName as String
  • Variable should be declared before the code using it in the appropriate code area
  • Can also be set by typing OPTION EXPLICIT in the General area of the form



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
6. Advantages of using variables
  • Your code is easier to read
  • Misspelling variable names is caught by the complier
  • Makes it easier to set the scope
  • More professional



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
7. Scope
  • There are different levels where you define variables
    1. Procedure
    2. Form or general declarations
    3. Project level (global)



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
8. Description
  • Variables can be accessed only in specific parts of the program
  • Prevents errors especially in large programs
  • Allows for compartmentalization of the program
  • Variables are only available to those parts of the program that need it



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
9. Law Enforcement
  • Scope can be compared to different types of law enforcement:
    local = same as local police
    general or form = county sheriff
    global = state police



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
10. General (form) Level Variables
  • Are declared in the general area of the form
  • Are available to all objects and code windows attached to the form
  • The value of the variable is accessible to any code in the form
  • NOT the same as a global variable



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
11. Object Level Variables
  • Are declared in the code window of the specific object
  • Exists only when that code is running
  • If the same variable name is used in a different code window, they are DIFFERENT variables
  • Also called local variables and are preferred when possible



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
12. Which should you use?
  • If only one object will use the information stored in the variable, make it Object Level
  • If more that one object needs it, make it Form Level…more later



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
13. Type Conversion Functions
  • This allows you to convert from one data type to another
  • Val - grabs a numeric value from a string
  • Str$ (Str) converts a numeric value to a string
  • Is frequently used to convert from variant to a more specific type



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
14. Simple Dialog Boxes
  • Message Box (MsgBox) is a easy way to send a short message to a user
  • Syntax: MsgBox ("String value to send")
  • Example: MsgBox ("Hello World")
  • Or: Dim Message as String
      Message = "Hello World"
       MsgBox (Message)



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
15. Inputbox
  • These are easy ways to get quick input from the user
  • Syntax: VariableName = Inputbox(String value representing questions)
  • Example: Dim UserName as String
        UserName = Inputbox ("What is your name?")
        or , Dim Answer as Integer
        Answer = Inputbox ("What is 5 + 3 ?")



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
16. If - Then - EndIf
  • Allows for simple evaluations
  • Syntax: IF expression THEN command
  • Example: If Name = "Wally" then MsgBox ("You're a Nerd")
    If Answer <> 12 then MsgBox ("Sorry, 5 + 7 = 12")



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
17. Block for of IF Else EndIF
  • IF expression THEN
       code
    ELSE
       more code
    END IF



































CSCI N331 Visual Basic: n331/vb04VariablesandScope
18. Example:
  • IF Ans4 = Total THEN
        MsgBox ("Very Nice!")
    ELSE
        MsgBox ("Too Bad !")
    END IF



































outline

Variable Description

Principal Uses

Type of Variables

Variable Declaration Modes

Explicit Declaration

Advantages of using variables

Scope

Description

Law Enforcement

General (form) Level Variables

Object Level Variables

Which should you use?

Type Conversion Functions

Simple Dialog Boxes

Inputbox

If - Then - EndIf

Block for of IF Else EndIF

Example: