Accessing an arraylist from multiple forms.

P

Paulers

Hello all,

I have 2 forms, form1 and form2. I am trying to store some objects in
an arraylist and have the arraylist be accessible by both forms. How
would I go about doing that? I think I might have to pass the arraylist
object to the second form so that it can access the objects inside the
arraylist but I am not sure what that looks like. any help is greatly
appreciated.

thanks
 
R

Ravimama

Hello Paulers,

Suppose you have alName as the array list in form1 which you want to
pass to form 2:
1. Declare the array list as Shared in form 1.
2. Access the arraylist of form 1 in form2 using: form1.alName
 
O

Oenone

Paulers wrote:

In your second form, add the following to the declarations:

\\\
Public myArrayList As ArrayList
///

When opening your second form from within your first form, set the field you
have declared as follows:

\\\
Dim myForm2 As New Form2

myForm2.myArrayList = arrayListFromForm1

myForm2.ShowDialog()
///

Now from within Form2 you can access the ArrayList however you wish using
the myArrayList object reference. Be aware that it is the actual same
ArrayList object as used by Form1, not a copy, so any changes you make to
its items will be seen by Form1 too.

Hope that helps,
 
C

Cor Ligthert [MVP]

Paulers,

In my idea does this completely depends on your design.

The first two answers have to do with a situation where there is a main form
and a kind of dialog form.

The answer from Theo is based that your arraylist has to be persistent in
your program undepended if there are active forms.

Cor
 
P

Paulers

I tried this but Form2 does not know what Form1 is. It says it is not
declared.
 
B

Brendon Bezuidenhout

Have you tried creating a new constructor for Form2 to take the ArrayList
ByRef?
 
R

Ravimama

Hey Paulers,

Did you try the solution I had given. As per the situation you have
given it should work. Its very simple.


Ravi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top