CSCI 220 Programming With Visual Basic Lecture 4- Variables and Scope

Variables
    Description
        A container for information
        A name that refers to a chunk of computer memory
        Variables have Values
        Name can be used in statements rather than value
        Value can change
    Principal uses
        Store information gotten from user, environment, or another source 
        Work with information stored in object properties
       Manipulate information (info garage)
    Types
        Computer stores different types of information in different ways
        (Chart on page 105 of book)
        Strings : text
            Fixed or dynamic length
        Integers :Whole and Negative Numbers
            Integers and Long Integers
        Real Numbers: Numbers with decimal values
            Single and Double Precision
        Currency
            8 digits + four decimal places 
        Variant
            Computer takes its best guess.  Risky
    Type conversions
        Allow you to convert from one data type to another
        Val - Graps a numeric value from a string
        Str$ - Converts a numeric value to a string
        Frequently used to convert from variant to more specific type
    Variable Declaration Modes
        Implicit
            Simply by using variable, you declare its existence
            Used by older forms of BASIC
            Only way of specifying type was type declaration characters ($%&#, etc)
            No way to check spelling of variable names -- common errors
        Explicit
            Each variable must be declared in a DIM statement
            Syntax:   Dim VariableName As Type
            Example: Dim UserName As String
            Varaibles should be declared before code in appropriate code area
            Set up in Environment dialog of Options Menu:  Require Variable Declaration
            Can also be set by typing OPTION EXPLICIT in General area of form
            Advantages:
                Code is easier to read
                Mispelling variable names is caught by compiler
                Easier to set scope
                More professional

Scope
    Description:
        Variables can be accessed only in specified parts of the program
        Prevents errors -esp. in large programs
        Allows for compartmentalization of program.
        Variables are available only to those parts of the program that need it
    Form - Level Variables
        Declared in general area of form
        Available to all objects, code windows attached to form
        Value of variable accessible to any code in form
    Object - Level Variables
        Declared in code window of a specific object
        Exists only when that code is running.
        If same variable name is used in a different code window, they are DIFFERENT variables.  
        Also called local variable
        Preferred when possible.
    Which to use?
        If only one object will use the info stored in the variable, make it Object Level
        If more than one object needs it, make it Form Level
    More to come.....

Simple dialog Boxes:
    MsgBox
        Easy way to send short message to user
        Syntax :     MsgBox (String Value to send)
        Examples:  MsgBox ("Hi there")
            Dim Message as String
            Message = "Hello, world!"
            MsgBox (Message)
    Inputbox and Inputbox$
        Easy way to get quick input from user
        Inputbox gets numeric value, Inputbox$ gets string
        Syntax:   VariableName = Inputbox$(String value representing question)
       Example:
            Dim UserName as String
            UserName = inputbox$("What is your name?")
            or
            Dim Answer as Integer
            Answer = Inputbox("What is 5+3?")

IF - THEN - ENDIF
    Allows simple evaluations
    Simple form
    Syntax:  	IF Expression THEN Command
    Example:
        If Name = "Wally" then MsgBox("You're a NERD")
        If Answer <> 12 then MsgBox ("Sorry.  5+7 = 12")
    Block Form
    Syntax:
          
        IF Expression THEN
            Code
        ELSE
            More Code
        END IF

Example:
    If Ans4 = Total Then
        MsgBox ("Very Nice!")
    Else
        MsgBox ("Too Bad!)
    End if

Your project, The Math Machine (VB4)


Purpose:
The purpose of this program is to illustrate your understanding of variable 
declaration principles, the use of input and message boxes, and basic 
understanding of variable scope

Description: 
The program will begin with an input box asking the user for her name.  
After entering a value, the user will be shown a screen with two text 
boxes, one for a value of X and one for a value of Y.  The screen will 
also show some simple formulas manipulating X and Y:  X+Y, X-Y, X*Y, X/Y. 
Initially there will be no values shown by these labels.  There should 
be some sort of calculate button on the form.  When the user chooses to 
calculate, the formulas will show the calculated values.  When the user
quits, she should receive a goodbye message that includes her name.

Procedure:
  Developing interface
    Write your own sketch for this one.  Decide what it will look like, then 
    build it.
    My version had ten labels, 2 text boxes, and 2 command buttons.  
    Name any controls that will contain code or be referred to by code

  Setting up
    Go to the general area of the form
    Set explicit variable declaration  (by typing Option Explicit)
    Type in your general remark statements

  Getting the user's name
     When will this happen in the program's execution?
     Where should you put the code so this happens?
     What kind of function should you invoke to get the user's name?
      Which type of variable will the user's name go into?
      What should the SCOPE of that variable be?
      Will another procedure use the value of this variable?
      Don't write any code for this section until you have answered ALL 
      the questions above.
      Dimension the variable in the proper place
      Write the code in the proper place
    
  Writing the calculation code
    Where will the code go that does all the calculations 
    (Be careful here!  Think!!!)
    Which variables will this code need?
    What scope should those variables have?
    Write the code that handles the addition operation
    Save, test, and debug
    Continue with the other operations until they all work
    
  The Quit Button   
    This is a little trickier than usual.  (Great, mess with the one 
    thing we know... (Grin!))
    When the user wants to quit, send a dialog box that says goodbye 
    and uses their name.
    If the name doesn't work, check the variable name and scope.

Have FUN!!!!!!!