Creating "x" amount of objects at runtime...please help!!

A

almurph

Hi,

I'm trying to create a "certain" amount of ArrList() objects, lets
say "X" amount but I do not know how many there will be at compile
time, only at runtime.

I need some sort of mechanism that can do this like:

ArrayList() AR1 = new ArrayList();
ArrayList() AR2 = new ArrayList();
ArrayList() ARn = new ArrayList();

where "n" is the max number of ArrayList() objects to be created -
which I do not know at compile time.

I would greatly appreciate any suggestions/comments/advice/code-
sample that you may be able to offer me.

Thank you,
Al.
 
A

Anthony Jones

Hi,

I'm trying to create a "certain" amount of ArrList() objects, lets
say "X" amount but I do not know how many there will be at compile
time, only at runtime.

I need some sort of mechanism that can do this like:

ArrayList() AR1 = new ArrayList();
ArrayList() AR2 = new ArrayList();
ArrayList() ARn = new ArrayList();

where "n" is the max number of ArrayList() objects to be created -
which I do not know at compile time.

I would greatly appreciate any suggestions/comments/advice/code-
sample that you may be able to offer me.

I wish I could earn a penny every time this sort of question was asked ;)
The fundemental answer is use another array to hold your list of arrays.

It helps if you define your version. I see little real use for ArrayList in
..NET 2.0 or higher. Assuming C# 2:-

List<ArrayList> AR = new List<ArrayList>();
for(int i = 0; i < n; i++) //where n is a variable holding the number of
ArrayLists you need
AR.Add(new ArrayList());

You now access specific ArrayLists as AR[0], AR[1] etc.
 
G

Geoffrey Summerhayes

Hi,

        I'm trying to create a "certain" amount of ArrList() objects, lets
say "X" amount but I do not know how many there will be at compile
time, only at runtime.

        I need some sort of mechanism that can do this like:

ArrayList() AR1 = new ArrayList();
ArrayList() AR2 = new ArrayList();
ArrayList() ARn = new ArrayList();

        where "n" is the max number of ArrayList() objects to be created -
which I do not know at compile time.

        I would greatly appreciate any suggestions/comments/advice/code-
sample that you may be able to offer me.

Not seeing the forest for the trees.
Store the ArrayLists in an ArrayList.
 
B

Ben Voigt [C++ MVP]

Geoffrey Summerhayes said:
Not seeing the forest for the trees.
Store the ArrayLists in an ArrayList.

But then you have "X + 1" ArrayLists!

I think it's much more instructive to use each ArrayList to hold the next
one. Naturally you need a place to hold the first ArrayList as well, so put
it in the last one (which would otherwise still be empty). And then put one
of the ArrayLists also in the debugger watch window and verify that you can
get to all the contents.
 
T

Tom P.

Not seeing the forest for the trees.
Store the ArrayLists in an ArrayList.

But then you have "X + 1" ArrayLists!

I think it's much more instructive to use each ArrayList to hold the next
one.  Naturally you need a place to hold the first ArrayList as well, so put
it in the last one (which would otherwise still be empty).  And then put one
of the ArrayLists also in the debugger watch window and verify that you can
get to all the contents.



Doesn't .NET have a LinkedList class?

That seems like a no-brainer.
Tom P.
 
J

Jeff Johnson

Not seeing the forest for the trees.
Store the ArrayLists in an ArrayList.

But then you have "X + 1" ArrayLists!

I think it's much more instructive to use each ArrayList to hold the next
one. Naturally you need a place to hold the first ArrayList as well, so
put
it in the last one (which would otherwise still be empty). And then put
one
of the ArrayLists also in the debugger watch window and verify that you
can
get to all the contents.


Doesn't .NET have a LinkedList class?

Was that a pun?
 
F

Family Tree Mike

Hi,

I'm trying to create a "certain" amount of ArrList() objects, lets
say "X" amount but I do not know how many there will be at compile
time, only at runtime.

I need some sort of mechanism that can do this like:

ArrayList() AR1 = new ArrayList();
ArrayList() AR2 = new ArrayList();
ArrayList() ARn = new ArrayList();

where "n" is the max number of ArrayList() objects to be created -
which I do not know at compile time.

I would greatly appreciate any suggestions/comments/advice/code-
sample that you may be able to offer me.

Thank you,
Al.


My choice:

Dictionary<string, ArrayList> pile = new Dictionary<string,ArrayList>();
int N = 5;

for (int idx = 1; idx <= N; ++idx)
pile.Add(string.Format("AR{0}", idx), new ArrayList());

Then use:
pile ["AR3"].Whatever....
 
G

Göran Andersson

Hi,

I'm trying to create a "certain" amount of ArrList() objects, lets
say "X" amount but I do not know how many there will be at compile
time, only at runtime.

I need some sort of mechanism that can do this like:

ArrayList() AR1 = new ArrayList();
ArrayList() AR2 = new ArrayList();
ArrayList() ARn = new ArrayList();

where "n" is the max number of ArrayList() objects to be created -
which I do not know at compile time.

I would greatly appreciate any suggestions/comments/advice/code-
sample that you may be able to offer me.

Thank you,
Al.

Let's say that you actually did manage to create variables dynamically,
how would you use them?

None of the variables are defined at compile time, so you can't use any
of them in the code...

As usual when someone asks a question like this, the answer is that you
should rather ask about what it is that you are trying to accomplish
rahter than asking about how you think that it should be solved.

Unless you are stuck with framework 1, you should not use the ArrayList
class at all. Use a generic List instead.
 
A

Arne Vajhøj

Ben said:
But then you have "X + 1" ArrayLists!

I think it's much more instructive to use each ArrayList to hold the
next one. Naturally you need a place to hold the first ArrayList as
well, so put it in the last one (which would otherwise still be empty).
And then put one of the ArrayLists also in the debugger watch window and
verify that you can get to all the contents.

If you want to code in C in C#, then that is the way to go.

But it is not very nice code, because you will be storing
two different types in the same ArrayList.

And when the chance to switch to .NET 2.0 and use List<List<X>> comes
it will simply not work.

Arne
 
B

Ben Voigt [C++ MVP]

Arne Vajhøj said:
If you want to code in C in C#, then that is the way to go.

Gee. I was trying to get the OP to think past "I need lots of lists". By
giving an infinitely self-referential use of "lots of lists" albeit totally
useless. And also to see whether the OP would actually try to see how
deeply the debugger would expand his structure of lists.
 
T

Todd Carnes

I think it's much more instructive to use each ArrayList to hold the next
one. Naturally you need a place to hold the first ArrayList as well, so
put it in the last one (which would otherwise still be empty). And then
put one of the ArrayLists also in the debugger watch window and verify
that you can get to all the contents.

Isn't that just a complicated way to end up with a simple linked list?
 

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