Introduction to Visual BASIC (CS 143)

Global Variables

What is a local variable

  • A variable (identifier) whose use is local to a given object or method
  • When a variable is declared (DIM statement) inside a method, it is local to that method

What is a global variable?

  • An variable (identifier) whose use is global to a given object or method
  • When a variable is declared outside of a method, it is global to that method

How do I declare a variable

Dim variable_identifier As variable_datatype

    Example: Dim iNumber As Integer
 

How could I use global variables in a program

'Declare variable "by itself"
Dim iNumber As Integer                                                                

Private Sub frmGlobal_Load()
    ' Assign a value to the global variable iNumber
     iNumber = 10
End Sub                                                                                        

Private Sub cmdNumber_Click()
    ' Access abd display the value of the global variable iNumber
    lblNumber.Caption = Val(iNumber)
End Sub