I need some help. I have been problems getting my programs to recognize variables outside of the subroutines even if I make them public. Ive been able to work around the problem until now. I am working with arrays. It wont let me declare the array outside the subroutine and it says the array is not declared. If i put it in the first subroutine the second subroutine doesnt recognize the first array. Can anyone help me?

Public Class Form1

    Public Sub getbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getbtn.Click
        Dim q(5) As String
        q(0) = "Declare xyz as an integer"
        q(1) = "Declare bottom as a string"
        q(2) = "What property needs to be adjusted to affect the order users tab to controls in"
        q(3) = "Declare number as a random object"
        q(4) = "Declare Doppleganger as a double"
        Dim r As New Random()
        Dim nextquestion As Integer
        nextquestion = r.Next(0, 5)
        answtxt.Text = q(nextquestion)

    End Sub
    Private Sub enterbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterbtn.Click
        Dim a(5) As String
        a(0) = "Dim xyz As Integer"
        a(1) = "Dim bottom As String"
        a(2) = "TabIndex"
        a(3) = "Dim number As New Random()"
        a(4) = "Dim doppleganger As Double"

        Dim question As String
        Dim ans, rightanswer As String
        question = answtxt.Text
        ans = questtxt.Text
        If question = q(o) Then
            rightanswer = a(0)
        ElseIf question = q(1) Then
            rightanswer = a(1)
        ElseIf question = q(2) Then
            rightanswer = a(2)
        ElseIf question = q(3) Then
            rightanswer = a(3)
        ElseIf question = q(4) Then
            rightanswer = a(4)
        End If

        If ans = rightanswer Then
            MsgBox("Fantastic!")
        Else
            MsgBox("Wrong, form should be as in this example: Dim x As Integer")
            answtxt.Focus()
        End If
    End Sub
End Class

Declare your variable in the class itself. It will be a class-level variable and every procedure in the class can access it unless you "hide" it with a procedure level variable. This is called the scope of the variable

Public Class Form1

  Private q(5) As String ' This variable's scope is this class. 

  Public Sub getbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getbtn.Click
    Dim q(5) As String ' This variable's scope is getbtn_Click procedure. It "hides" the class-level q()-variable
    ' If you DON'T declare this variable, procedure's code uses the class level q() which is also seen by enterbtn_Click
    .
    .
    .
  End Sub

  Private Sub enterbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' This procedure sees the class level q() variable

  End Sub

End Class

I suggest you do some googling about variable scopes in Visual Basic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.