Use a variable to call an arraylist

L

lgbjr

Hi All,

I have 10 arraylists in a VB.NET app. Based on the state of various
checkboxes and radiobuttons, a set of data is read into these arraylists. I
don't know how many of the arraylists will be used until runtime. what I'd
like to do is something like this:

Dim AL1 as New ArrayList
...
Dim AL10 as New ArrayList
Dim alName as string
Dim i, j as Integer
i = some number between 1-10 '(determined by user selections)
For j = 1 to i
alName="AL"+j.ToString
' ***Something that uses the value of alName to call the appropriate
ArrayList***
' ***Code to add the right stuff to the right ArrayList***
Next j

does that make sense? I know I can use a bunch of If..Then..ElseIfs, but I
thought the above method would be a bit more elegant.

TIA
Lee
 
L

lgbjr

It's obvious that I haven't had enough caffiene yet!!

I don't know how DirectCast(alName,ArrayList).DoArrayListStuff slipped my
mind!

cheers
Lee
 
C

Cor Ligthert [MVP]

Lee,

A string adding to an arraylist do you with an Add(as with all Ilist
implementing collections)

\\\
Al1.Add(ALName)
///

I hope this helps,

Cor
 
L

lgbjr

hi Cor,

You misunderstood what I am trying to do. Based on a number of controls on a
form, I have to fill between 1 and 10 arraylists with information. Actually
filing the arraylist is easy (as you said AL1.Add(something)). the problem
is not knowing how many to fill until runtime. So, I declare 10 arraylists
(AL1 to AL10). Based on the form controls, I get a value for i(integer)
between 1 and 10.

what I want to do is this:

For j = 1 to i
alName="AL"+j.ToString 'This gives me my Arraylist name
DirectCast(alName,ArrayList).Add(Stuff for AL# arraylist)
Next j

but the DirectCast gives me an InvalidCastException

Now, what I am doing is:

For j = 1 to i
If j=1 then
AL1.Add(Stuff)
Elseif j=2 Then
AL1.Add(Stuff)
AL2.Add(More Stuff)
ElseIf j=3 Then
Al1.Add(Stuff)
Al2.Add(MoreStuff)
Al3.Add(EvenMoreStuff)
..
ElseIf j=10
..
EndIf
Next j

Using a Directcast seems more efficient from a coding point of view, I just
don't know if I can do a DirectCast from a value in a string variable to an
arraylist.

does that make sense?

Cheers
Lee
 
C

Cor Ligthert [MVP]

Lee,

I have the idea that you try to find a difficult solution for something
simple as this.
\\\
Dim a(9) As ArrayList
For i As Integer = 0 To 9
a(i) = New ArrayList
Next
///
\\\
For j As Integer = 1 To i
a(j).Add(stuff for table)
Next
///

What you try to do has nothing to do with casting by the way. You can use
reflection for it, however probably than as I wrote only a difficult
solution for an easy problem.

I hope this helps,

Cor
 
L

lgbjr

Hi Cor,

Thanks for the advice. It works great. But, I don't really understand why!

What does Dim a(9) As ArrayList actually do? To me, it looks like it's
declaring an ArrayList with an upper bound of 9, similar to:
Dim a(9) as String creating an array. Obviously, that's not what it does.

If you can explain what the Dim a(9) As ArrayList actually does, I think the
rest of it is pretty easy! I've just never seen that done before.

Thanks Again,
Lee
 
H

Herfried K. Wagner [MVP]

lgbjr said:
What does Dim a(9) As ArrayList actually do? To me, it looks like it's
declaring an ArrayList with an upper bound of 9, similar to:
Dim a(9) as String creating an array. Obviously, that's not what it does.

'Dim a(9) As ArrayList' creates an array variable which can hold 10
references to 'ArrayList' objects (indices 0 to 9). Declaring the array
variable doesn't assign references to instances of 'ArrayList' to the
array's items automatically. That's why you have to do that later, for
example in a 'For' loop as demonstrated in Cor's sample.
 
L

lgbjr

Thanks Herfried,
Now I understand!

Herfried K. Wagner said:
'Dim a(9) As ArrayList' creates an array variable which can hold 10
references to 'ArrayList' objects (indices 0 to 9). Declaring the array
variable doesn't assign references to instances of 'ArrayList' to the
array's items automatically. That's why you have to do that later, for
example in a 'For' loop as demonstrated in Cor's sample.
 

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