Accessing an arraylist from multiple forms.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulers

    #1

    Accessing an arraylist from multiple forms.

    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

  • Ravimama

    #2
    Re: Accessing an arraylist from multiple forms.

    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


    Paulers wrote:
    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

    Comment

    • Oenone

      #3
      Re: Accessing an arraylist from multiple forms.

      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.myArray List = arrayListFromFo rm1

      myForm2.ShowDia log()
      ///

      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,

      --

      (O)enone


      Comment

      • Theo Verweij

        #4
        Re: Accessing an arraylist from multiple forms.

        Declare the arraylist in a module.

        Paulers wrote:
        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
        >

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Accessing an arraylist from multiple forms.

          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

          "Paulers" <SuperGh0d@gmai l.comschreef in bericht
          news:1162624428 .438507.94310@e 3g2000cwe.googl egroups.com...
          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
          >

          Comment

          • Paulers

            #6
            Re: Accessing an arraylist from multiple forms.

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

            Ravimama wrote:
            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
            >
            >
            Paulers wrote:
            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

            Comment

            • Brendon Bezuidenhout

              #7
              Re: Accessing an arraylist from multiple forms.

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


              "Paulers" <SuperGh0d@gmai l.comwrote in message
              news:1162660647 .377358.55450@h 54g2000cwb.goog legroups.com...
              >I tried this but Form2 does not know what Form1 is. It says it is not
              declared.
              >
              Ravimama wrote:
              >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
              >>
              >>
              >Paulers wrote:
              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
              >

              Comment

              • Ravimama

                #8
                Re: Accessing an arraylist from multiple forms.

                Hey Paulers,

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


                Ravi


                Brendon Bezuidenhout wrote:
                Have you tried creating a new constructor for Form2 to take the ArrayList
                ByRef?
                >
                >
                "Paulers" <SuperGh0d@gmai l.comwrote in message
                news:1162660647 .377358.55450@h 54g2000cwb.goog legroups.com...
                I tried this but Form2 does not know what Form1 is. It says it is not
                declared.

                Ravimama wrote:
                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
                >
                >
                Paulers wrote:
                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

                Comment

                Working...