How to create an array of ArrayList object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnee
    New Member
    • Jul 2007
    • 11

    How to create an array of ArrayList object

    I receive a troubleshooting : "Use the New keyword to create the instance" when creating an array of ArrayList object.
    Below is the code:

    Dim arrSmallArrays( ) As System.Collecti ons.ArrayList
    For iItem = 0 to iMax
    arrSmallArrays( iItem) = New System.Collecti ons.ArrayList
    Next

    Please show me what is wrong in the code.

    Thank you very much.
  • sendhiltam
    New Member
    • Jul 2007
    • 6

    #2
    'Initialize the Array list

    Dim arrSmallArrays As New System.Collecti ons.ArrayList

    For iItem = 0 to iMax
    'Add your variables(iItem ) through Add property of Arraylist
    arrSmallArrays. Add(iItem)
    Next

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Are you making an array of arraylists or an arraylist of arraylists?

      C#
      Code:
      //An array of 10 ArrayLists
      ArrayList[] mydblarray= new ArrayList[10];
      for (int i=0;i<10;i++)
      {
         mydblarray[i]=new ArrayList();
      }
      C#
      Code:
      //a single ArrayList that contains 10 more ArrayLists
      ArrayList mydblarray= new ArrayList();
      for (int i=0;i<10;i++)
      {
         mydblarray.Add(new ArrayList());
      }

      Comment

      • jinnee
        New Member
        • Jul 2007
        • 11

        #4
        I want to manage a two-dimension array (like grid).
        All horizontal items (on the same row) will be mapped to an ArrayList.
        For that reason I create an array of ArrayList objects, called arrSmallArrays( ).

        When I try to create arrSmallArray(i ) by using New, I get that error. I am using VB.NET.

        Thanks for your supports.

        Comment

        • RoninZA
          New Member
          • Jul 2007
          • 78

          #5
          Jinnee...

          I think you're complicating things for yourself by using ArrayLists in a 2-dimensional context. It gets a little hairy to manage the values, etc, so I would suggest you rather use a normal 2-dimensional array.

          If you want to dynamically change the dimensions of your array, remember to use the Preserve keyword in your ReDim statement!

          Comment

          • jinnee
            New Member
            • Jul 2007
            • 11

            #6
            Yes, because the data type of items is not identity, so I think ArrayList is a solution.
            Back to my problem. Please show me what is wrong if I implement the code as below. I hope through the explanation, I can learn some technique in VB.NET.

            arrSmallArrays( iItem) = New System.Collecti ons.ArrayList

            Thanks.

            Comment

            • vanc
              Recognized Expert New Member
              • Mar 2007
              • 211

              #7
              Originally posted by jinnee
              Yes, because the data type of items is not identity, so I think ArrayList is a solution.
              Back to my problem. Please show me what is wrong if I implement the code as below. I hope through the explanation, I can learn some technique in VB.NET.

              arrSmallArrays( iItem) = New System.Collecti ons.ArrayList

              Thanks.
              Since you want to use ArrayList and the idea to create ArrayList is to skip the boundary of the array, so it's weird if define an ArrayList with fixed length!!
              It means, you should not define a aArrayList[20].
              I know your problem is because you have not instantiated the first ArrayList :), it's fairly simple. The code for the first line when you declare arrSmallArrays As New....., but you didn't.
              Then if you want to add an ArrayList to the main ArrayList, the code should like:
              aChildArray As New ArrayList
              arrSmallArrays. Add(aChildArray )
              It should solve your problem.

              cheers.

              Comment

              • jinnee
                New Member
                • Jul 2007
                • 11

                #8
                Thanks Vanc.

                As Vanc's suggestion, I change the code and it works (see below). That means, in this case we can not use New to create instance of array. We must use Add() instead.(!?!)

                Dim arrSmallArrays As New System.Collecti ons.ArrayList
                For iItem = 0 To 8
                Dim arrSmallArray As New System.Collecti ons.ArrayList
                arrSmallArrays. Add(arrSmallArr ay)
                Next

                'To add value to cell, I have to use CType():

                If CType(arrSmallA rrays.Item(iIte m), ArrayList).Cont ains(value) = False Then
                CType(arrSmallA rrays.Item(iIte m), ArrayList).Add( value)
                End if

                By using CType(), the code looks quite prolix.

                Again, thank you very much for your help.

                Comment

                Working...