Question passing array from one sub to another

eyewittness

Member
Joined
Jan 26, 2009
Messages
9
Programming Experience
Beginner
How can i pass one array in one sub to another sub? Everytime when i do this name i is empty when i use the verify method!

My code is like this:
VB.NET:
Public Class name_check
    'data
    Dim name() As String
    Dim myStringElementsnameindex As Integer = 0
    'methoden
   'i put a name from an edit box into name
    Public Sub checkname(ByVal name As String)
        add_names_to_array(name)
    End Sub
    'add the name to the array
    Public Sub add_names_to_array(ByVal stringToAdd As String)
        ReDim Preserve name(myStringElementsnameindex)
        name(myStringElementsnameindex) = stringToAdd
        myStringElementsnameindex += 1
    End Sub

    'the name(i) is empty here!
    Public Sub Verify_name(ByVal name As String)
        Dim i As Integer
        Dim index_arraynr As Integer
        For i = 0 To name.Length
            If name(i) = "mike" Then
                index_arraynr = i
                If name(index_arraynr) = "mike" Then MessageBox.Show("Succesvol geverifeerd") _
                 Else MessageBox.Show("wrong name submitted")
                Exit Sub
            End If
        Next

    End Sub
End Class
 
1. You should be getting error message about trying to create an array called 'name'. Change it to names() or something equally descriptive.

2. Your Verify_name definition is wrong. You should be passing an array of strings into your sub.

VB.NET:
Public Sub Verify_name(ByVal names[B]()[/B] As String)

3. You've got your names() array dimensioned at the class level. You don't need to pass the array in the way you've got it set up.
 
allright, can you give me an example its like this:

one edit box
2 buttons

on button 1 add text to an array ( method 1)

on button 2 verify the text to the array, if it's in the array then showmessagebox(method2)

Then its clear to me.
I need to make use of a class as much as possible.
 
Since you're using framework 2.0 I'd suggest a List(Of String) so you don't need to ReDim Preserve each time.

VB.NET:
Public Class Form1

	Dim names As New List(Of String)

	Private Sub uxAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxAdd.Click

		If uxName.Text <> String.Empty Then
			names.Add(uxName.Text)
			uxName.ResetText()
		End If

	End Sub

	Private Sub uxVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxVerify.Click

		For i As Integer = 0 To names.Count - 1
			If names(i).ToLower = "mike" Then
				MessageBox.Show("Mike found @ index " & i)
				Exit Sub
			End If
		Next

		MessageBox.Show("Mike not found!")

	End Sub
End Class
 
Button 2 is on another form called form2.
Then i try this:
VB.NET:
Private Sub uxVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxVerify.Click

		For i As Integer = 0 To form1.names.Count - 1
			If form1.names(i).ToLower = "mike" Then
				MessageBox.Show("Mike found @ index " & i)
				Exit Sub
			End If
		Next

		MessageBox.Show("Mike not found!")

	End Sub
form1.names.Count is empty, it doenst pass the stringlist.
 
Button 2 is on another form called form2.

And you didn't find this relevant in the initial information you provided?

form1.names.Count is empty, it doenst pass the stringlist.

You shouldn't even have access to form1.names the way you have it set up. You should be getting an error message to the tune of Names is not a member of form1.

There are several ways to get the data from Form1 to Form2.

One way would be to use a constructor.

Form2:

VB.NET:
	Dim passedNames As New List(Of String)

	Public Sub New(ByVal names As List(Of String))

		InitializeComponent()
		passedNames = names

	End Sub

VB.NET:
		For i As Integer = 0 To passedNames.Count - 1
			If passedNames(i).ToLower = "mike" Then
				MessageBox.Show("Mike found @ index " & i)
				Exit Sub
			End If
		Next

		MessageBox.Show("Mike not found!")

Form1

VB.NET:
	Private Sub uxSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxSwitch.Click

		Dim frm2 As New Form2(Names)
		frm2.Show()
		Me.Hide()

	End Sub

Another way would be to create a ReadOnly property on Form1 so that you can reference it from Form2

VB.NET:
	Private _names As New List(Of String)
	Public ReadOnly Property Names() As List(Of String)
		Get
			Return _names
		End Get
	End Property

VB.NET:
		For i As Integer = 0 To Form1.Names.Count - 1
			If Form1.Names(i).ToLower = "mike" Then
				MessageBox.Show("Mike found @ index " & i)
				Exit Sub
			End If
		Next

		MessageBox.Show("Mike not found!")
 
Back
Top