473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Use a variable to call an arraylist

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
Nov 21 '05 #1
8 1254
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

"lgbjr" <lg***@nospam.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #2
Hmmm,

The DirectCast doesn't work.

any Ideas?

thanks
Lee

"lgbjr" <lg***@nospam.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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

"lgbjr" <lg***@nospam.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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


Nov 21 '05 #3
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

"lgbjr" <lg***@nospam.com> schreef in bericht
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hmmm,

The DirectCast doesn't work.

any Ideas?

thanks
Lee

"lgbjr" <lg***@nospam.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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

"lgbjr" <lg***@nospam.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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



Nov 21 '05 #4
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

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

"lgbjr" <lg***@nospam.com> schreef in bericht
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hmmm,

The DirectCast doesn't work.

any Ideas?

thanks
Lee

"lgbjr" <lg***@nospam.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
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

"lgbjr" <lg***@nospam.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
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



Nov 21 '05 #5
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
Nov 21 '05 #6
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
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:OV**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #7
"lgbjr" <lg***@nospam.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
Thanks Herfried,
Now I understand!

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"lgbjr" <lg***@nospam.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TS | last post by:
Hello, I am trying to have a member of a class declared as an arrayList to have get & set property accessors, but I can't get it to compile. This member variable can't be a simple array because I...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
3
by: Hrvoje Voda | last post by:
public bool Logon() { if (db!=null) { Login lg = new Login (db);
14
by: Shelby | last post by:
Hi, how do I set the Type of a variable? For example: Dim myhash as hastable ' I want to set the Type in myhash as String or Integer or even my own custom Structure.
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
11
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
0
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I want to display a progress bar on my web page. I have this code for progress bar in javascript in my web page. I have a button on the web page, if someone clicks that button...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.