Thursday, January 21, 2016

Variable Scoope on Visual Basic .NET



In proramming You will with scoope of variable. Scoope of variable can be localprivate or  public.
Here it is example of scoope variable use in modul and form
                                                                                                                                             
modul code:
Module Module1

  Public sNama As String

End Module


form Code:
Public Class Form1

Private sKota As String

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

  sNama = "Visual basic Net"
  TextBox1.Text = sNama

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

  sKota = "Medan"
  TextBox1.Text = sKota

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

  Dim sNegara As String = "Indonesia"
  TextBox1.Text = sNegara

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

  TextBox1.Text = snegara

End Sub

End Class


Di bawah ini adalah gambar demo programnya:


sNegara is stated as error  Name “snegara” is not declared.
this is caused by  sNegara 
variable as mentioned with Dim sNegara As String = "Indonesiaat Button3_click only have local scoope. cannot read from other sub prosedur (Button4_click).
ok