PDA

View Full Version : variable not declared! help w/ VB



indiasfinest
January 1st, 2007, 02:33 PM
so here is what it looks like:
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim subjectnumber As Integer = 0
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If
TextBox1.Text = "Subject One"
subjectnumber += 0
Else : subjectnumber += 1
End If
End Sub

Private Sub TextBox28_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox28.TextChanged
TextBox28.Text = subjectnumber
End Sub
End Class

i got an error on subjectnumber when using it, what am i doing wrong?

tucker
January 1st, 2007, 06:08 PM
Youre dimming subject number in Form1_Load, dim it in declarations. Since its a local variable it only lasts for the routine. If you dim it in declarations its module level and can be accessed anywhwere in the module.

indiasfinest
January 1st, 2007, 09:07 PM
i apologize for the amateur question but where is declarations/how do i access it.

skOOb
January 2nd, 2007, 02:21 PM
You are out of scope.

Public Class Form1
' declarations are made inside the class and outside any methods.
' so now you can use subjectnumber in all of your methods. e.g.
Public subjectnumber As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If
TextBox1.Text = "Subject One"
subjectnumber += 0
Else : subjectnumber += 1
End If
End Sub

Private Sub TextBox28_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox28.TextChanged
TextBox28.Text = subjectnumber
End Sub
End Class

indiasfinest
January 2nd, 2007, 05:31 PM
thanks for the help man