469,352 Members | 1,892 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

How to create an array of ArrayList object

11
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.Collections.ArrayList
For iItem = 0 to iMax
arrSmallArrays(iItem) = New System.Collections.ArrayList
Next

Please show me what is wrong in the code.

Thank you very much.
Jul 18 '07 #1
7 8062
'Initialize the Array list

Dim arrSmallArrays As New System.Collections.ArrayList

For iItem = 0 to iMax
'Add your variables(iItem) through Add property of Arraylist
arrSmallArrays.Add(iItem)
Next
Jul 18 '07 #2
Plater
7,872 Expert 4TB
Are you making an array of arraylists or an arraylist of arraylists?

C#
Expand|Select|Wrap|Line Numbers
  1. //An array of 10 ArrayLists
  2. ArrayList[] mydblarray= new ArrayList[10];
  3. for (int i=0;i<10;i++)
  4. {
  5.    mydblarray[i]=new ArrayList();
  6. }
  7.  
C#
Expand|Select|Wrap|Line Numbers
  1. //a single ArrayList that contains 10 more ArrayLists
  2. ArrayList mydblarray= new ArrayList();
  3. for (int i=0;i<10;i++)
  4. {
  5.    mydblarray.Add(new ArrayList());
  6. }
  7.  
Jul 18 '07 #3
jinnee
11
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.
Jul 19 '07 #4
RoninZA
78
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!
Jul 19 '07 #5
jinnee
11
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.Collections.ArrayList

Thanks.
Jul 20 '07 #6
vanc
211 Expert 100+
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.Collections.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.
Jul 20 '07 #7
jinnee
11
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.Collections.ArrayList
For iItem = 0 To 8
Dim arrSmallArray As New System.Collections.ArrayList
arrSmallArrays.Add(arrSmallArray)
Next

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

If CType(arrSmallArrays.Item(iItem), ArrayList).Contains(value) = False Then
CType(arrSmallArrays.Item(iItem), ArrayList).Add(value)
End if

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

Again, thank you very much for your help.
Jul 20 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Marco | last post: by
3 posts views Thread by John Haigh | last post: by
3 posts views Thread by Jack Addington | last post: by
14 posts views Thread by Web learner | last post: by
2 posts views Thread by Macca | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.