Variable scope

We have three sample programs illustrating the scope of a variable. A variable may be local to a single sub-program or global -- available to every sub-program on the form. The scope of a variable may be even wider, but, for now, we will restrict ourselves to form-level or module-level global variables. (Each form is defined in its own .vb file or module). Some programmers give module-level variables names starting with the letter "m" as a reminder of the scope of the variable.

Our first sample program has two local variables. They are local because the Dim statements that declare them are inside sub-programs (between the Private Sub and End Sub statements). Even though they both have the same name, strA, you can see that they are separate, each with its own value. Separate memory space is set aside for each of them.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strA As String
        strA = "Hello"
        Label1.Text = strA
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim strA As String
        strA = "Goodbye"
        Label1.Text = strA
    End Sub

Our second sample program has only one local variable, strA. Note that strA is re-created every time the sub-program executes. Even though the sub-program assigns the value "goodbye" to strA after displaying it, a new copy of strA has been created the next time the sub-program executes.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' A new copy of strA is created each time the sub-program is executed.
        ' strA disappears at the end of the sub-program execution
        Dim strA As String = "Hello"
        Label1.Text = strA
        strA = "goodbye"

    End Sub

Our third sample program has both a local and a global variable. Both are called StrA. One is global since it is declared outside of the event handlers. The other strA is declared locally inside the Button1_Click sub-program, so it is used instead of the global variable. When Button2_Click executes, the global strA is used. Separate memory space is set aside for each variable.

    ' This variable is global to the entire program.
    ' It is declared just below the Windows form designer code.
    Dim strA As String = "This variable is global."

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strA As String 'local to this sub program
        strA = "This variable is local."
        Label1.Text = strA
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Label1.Text = strA
    End Sub

Form-level global variables should be declared at the start of your programs, just below the code that is generated by the Windows Form Designer. You can use the keyword Private instead of Dim. Both have the same meaning in this case -- the variable is accessible to any sub program on this form.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.