I'm new to visual basic, and after writing my first few programs I decided it's a nice language for database front ends :-) just like VBA.

Everhing is working out fine, but there's one thing I can't figure out, how do I pass arrays between private subs without declaring them in a module?

12 Years
Discussion Span
Last Post by waynespangler

just like any other variable.
declare them in the first sub or global, on reference to the second sub put them between brackets, like this:

'Global declaration, variables can be used in entire script.
Dim strArray(1, 10) as String

Private Sub First()
'Local declaration, used in only this sub or referred to when calling subs.
Dim intArray(1, 10) as Integer
End Sub

Private Sub Second(intArray)
strArray(1) = "A"
intArray(1) = 2
End Sub


Try something like this:

Dim myArray(10) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        AddArray(myArray, Integer.Parse(TextBox2.Text))
        TextBox1.Text = myArray(Integer.Parse(TextBox2.Text)).ToString
    End Sub

    Private Sub AddArray(ByVal thisArray As Array, ByVal Index As Integer)
        thisArray(Index) = Index + 3
    End Sub

I didn't do it but check when passing arrays that your index is not outside the bounds of the array.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.