944,129 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 9741
  • VB.NET RSS
Oct 7th, 2005
0

Passing arrays between subs

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pieter is offline Offline
1 posts
since Oct 2005
Oct 19th, 2005
0

Re: Passing arrays between subs

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
molti is offline Offline
2 posts
since Oct 2005
Nov 1st, 2007
0

Re: Passing arrays between subs

Try something like this:
VB.NET Syntax (Toggle Plain Text)
  1. Dim myArray(10) As Integer
  2.  
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4. AddArray(myArray, Integer.Parse(TextBox2.Text))
  5. TextBox1.Text = myArray(Integer.Parse(TextBox2.Text)).ToString
  6. End Sub
  7.  
  8. Private Sub AddArray(ByVal thisArray As Array, ByVal Index As Integer)
  9. thisArray(Index) = Index + 3
  10. End Sub
I didn't do it but check when passing arrays that your index is not outside the bounds of the array.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: crystal report layout
Next Thread in VB.NET Forum Timeline: register page using SQL server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC