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?

Recommended Answers

All 2 Replies

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
Second(intArray)
'or
Second()
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.

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.