I am writing a simple program to understand the scope of global variable and timer.
Public Class Form1
Dim gobalVar As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
globalVar = 0
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Messagebox.Show(">" + globalVar.ToString)
globalVar = 1
Messagebox.Show("<" + globalVar.ToString)
End Sub
End Class
I set the timer to 5 seconds. so when the timer is trigger globalVar is 0, since it is set to 0 when the form is load, then after I set globalVar to 1, and the messagebox confirm it print <1 but the next time the timer is trigger the message box show >0, for some reason the globalVar is back to 0.
Shouldn't the globalVar be 1, since it is a global variable? am I declaring globalVar correctly as global variable in VB?