pass by reference

shiplu

New member
Joined
Aug 10, 2005
Messages
1
Programming Experience
1-3
Hi

I have got 2 forms. there is an arraylist in my first form. i want to pass it to the second form. second form need to get user input and store it in that data structure. so after user input when ok button will be clicked, the control will go back to the first form. and then form 1 can call other form and pass the arraylist..and so on.

can anyone give me an idea please?...thanks in advance.

cheers.
shiplu
 
Simply pass the ArrayList to the second form in its constructor or in a public property. Display the form by calling ShowDialog, and when the form is dismissed the ArrayList in the original form will contain any changes made in the second form. There is no need to pass the ArrayList by reference; passing by value is fine. This is because passing a reference-type object by value simply makes a copy of the reference, not the actual object, so both references still point to the same object.

Form1:
VB.NET:
	Private Sub SomeSub()
		Dim list As New ArrayList

		MessageBox.Show(list.Count.ToString())

		Dim dialogue As New Form2(list)

		dialogue.ShowDialog()

		MessageBox.Show(list.Count.ToString())
	End Sub
Form2:
VB.NET:
	Private m_List As ArrayList

	Public Sub New(ByVal list As ArrayList)
		'Call primitive constructor.
		Me.New()

		Me.m_List = list

		Me.m_List.Add(1)
		Me.m_List.Add(2)
		Me.m_List.Add(3)
	End Sub
 
Back
Top