Variable scope

This assignment tests your understanding of the scope of a variable and your ability to use local and global variables in a program.

Study our note on the scope of variables, and be sure you understand all three sample programs. If you are at all unclear, write the programs and experiment with them.

When you are sure you understand what is going on, turn in answers to the following questions. If I were you, I would answer them by studying the code, then check my answers by writing and testing the programs.

1. A user runs the following program:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim A As String
A = "hohoho"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim A As String
label1.text = A
End Sub
What will be displayed in label1 when the user clicks on Button1 followed by Button2?

Explain your answer using the terms "local" and "global" variable.

2. A user runs the following program:

Dim A As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim A As String
A = "hohoho"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
label1.text = A
End Sub

What will be displayed in label1 when the user clicks on Button1 followed by Button2?

Explain your answer using the terms "local" and "global" variable.

3. A user runs the following program:

Dim A As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
A = "hohoho"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
label1.text = A
End Sub
What will be displayed in label1 when the user clicks on Button1 followed by Button2?

Explain your answer using the terms "local" and "global" variable.


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.