How to create and reference an Arraylist at run-time

V

vbnewbie

I need to create a number of arraylists at run time.
The number of arraylists to be created is unknown at design time.
The name of the first newly created arraylist is the first element of
an array, NameArray (a Publicly declared array with an unknown number
of elements at design time).
The name of the second newly created arraylist is the second element of
NameArray, etc

Problem 1:
How can I retrieve that name from NameArray and assign it to the newly
created arraylist at run-time?
(I don't even know how to do this at design time, by the way)

Problem 2: the data in these arraylists needs to be accesible from
other parts (subs etc) of the code and additional data will be added to
those arraylists.

What is the best way to store lists of data accross the code?
(Maybe it is not an arraylist)

Keeping my fingers crossed, thanks!
 
H

HKSHK

Hello vbnewbie,

You could make an array of arraylists like this:


Dim al() As ArrayList

ReDim al(1) 'Here you would actually define how many arraylists
'you need.

al(0) = New ArrayList 'Create new Arraylists. In your program
al(1) = New ArrayList 'you might do that with a loop.

al(0).Add("Test") 'Add some values to the ArrayLists
al(1).Add("Test1")


You could e.g. define that the first entry in the ArrayList always
contains the name.

In this case I would make them public in a module.

Best Regards,

HKSHK
 
V

vbnewbie

I'm sorry, I am not sure how to make them public in a module since they
don't exist at run time.
Is there a type of module in which I could write public code?
 
S

Stephany Young

Yes. A Module.

Public MyArrayLists As New ArrayList

MyArrayLists will be available from anywhere in your project.
 
V

vbnewbie

Stephany said:
Yes. A Module.

Public MyArrayLists As New ArrayList

MyArrayLists will be available from anywhere in your project.
Thanks Stephany

Just to recap:

If I follow what HKSHK wrote before, I am creating an array of
arraylists.

I declare my array in a module: Public MyArrayLists() As ArrayList

I Redim my array at local level: ReDim MyArrayLists (aNumber)

I create my arraylists at local level: MyArrayLists(0) = New ArrayList,
etc

I add to my arraylists at local level: MyArrayLists(0).Add(Value)

Is that correct?
Thanks all for your patience...
 
S

Stephany Young

Nearly ...

You are creating an ArrayList of ArrayLists.

In a module, declare:

Public MyArrayLists As New ArrayList

It is important that it is done with the New keyword, otherwise your
ArrayList of ArrayLists will not be instantiated.

Because it is an ArrayList and NOT an array, ReDim is inappropriate.

At the local level you create create an ArrayList:

Dim _arraylist As New ArrayList

and then you add elements to it:

_arraylist.Add(Value)

and finally you add the local ArrayList to the 'global' ArrayList

MyArrayLists.Add(_arraylist)

Now, I hear you ask, how do I access the elements of MyArrayList and how do
I know which one is which?

The use of ArrayLists, as demonstrated, is only one way of doing it. There
are other ways, some of which are better than others.

For example, use of a Generic Dictionary object would allow you to implement
a solution where each element of the Dictionary is a name/value pair with
the value part being a single ArrayList. The elements of the Dictionary
would be accesible by the name you assigned at the time ou added the
element.

It's a matter of you researching these other techniques and deciding which
one is best for your solution.
 
C

Cor Ligthert [MVP]

vbNewbie,

In my idea is it better to describe what your problem is.

You probably think you are doing that, however you don't.

You are giving us your not working method and ask us to get it in a way it
can work even as that is rubish.

Cor
 
P

Phill W.

vbnewbie said:
I need to create a number of arraylists at run time.
The number of arraylists to be created is unknown at design time.
The name of the first newly created arraylist is the first element of
an array, NameArray (a Publicly declared array with an unknown number
of elements at design time).
The name of the second newly created arraylist is the second element of
NameArray, etc

Since you're creating lots of [ArrayList] objects and accessing them by
name, it sounds to me like you need a HashTable of ArrayLists.
You then access each each ArrayList by a unique Name, something like

Dim dict as New HashTable

For Each sName As String in NameArray
dict.Add( sName, New ArrayList )
Next

The only downside to this is that you need to cast the Object returned
by the HashTable into the ArrayList that you actually stored, as in

? DirectCast(dict.Item( "Fred" ), ArrayList).Count

VB'2005 has Generics, which get around this casting issue; I haven't
played with them yet, though.

HTH,
Phill W.
 
V

vbnewbie

Cor said:
vbNewbie,

In my idea is it better to describe what your problem is.

You probably think you are doing that, however you don't.

You are giving us your not working method and ask us to get it in a way it
can work even as that is rubish.

Cor
Cor,

I have not joined this group to explain what I don't know.
I am here to get the advice from people loke those who have responded
to my posts and in fact have helped me make my app work.
If you think it's rubish, save yourself some time, don't respond.

Thanks
 
V

vbnewbie

Stephany said:
Now, I hear you ask, how do I access the elements of MyArrayList and how do
I know which one is which?
You do!
I was already far enough in using the code with the array of arrayLists
when I read your post.
I'm not quite sure why but it seems like it is all working. I'll
probably hit a snag later and try the other methods suggested in this
discussion.
I used the Item property and IndexOf method to find and replace values
of elements and the RemoveAt method to remove elements in the
arraylists.

Thanks again for your help!
 
V

vbnewbie

Stephany said:
Now, I hear you ask, how do I access the elements of MyArrayList and how do
I know which one is which?
You do!
I was already far enough in using the code with the array of arrayLists
when I read your post.
I'm not quite sure why but it seems like it is all working. I'll
probably hit a snag later and try the other methods suggested in this
discussion.
I used the Item property and IndexOf method to find and replace values
of elements and the RemoveAt method to remove elements in the
arraylists.

Thanks again for your help!
 
V

vbnewbie

Thanks.
What are the main differences between a hashtable and a dictionary, and
why would you use one rather than the other?
 
C

Cor Ligthert [MVP]

Will you than use a better Nick than vbNewbie, I cannot see that it is you
in this way, there are more people using that nick. I surely will than do as
you wish and put you in my killbox.

Cor
 
P

Phill W.

vbnewbie said:
What are the main differences between a hashtable and a dictionary, and
why would you use one rather than the other?

Well, in VB'2003, this ...

Dim h as New Hashtable

.... compiles whereas this ...

Dim d as New Dictionary

doesn't.

You derive your own class from DictionaryBase, adding Strongly Typed
[Item] properties. Within one of these derived classes, you use

MyBase.Dictionary ...

to access the internal storage that holds the actual items for you, but
even this ("Dictionary") isn't of Type Dictionary; it's something else
that happens to Implement the IDictionary Interface ... ;-)

The biggest "problem" with Hashtables is that, when looping through
their contents, the items don't [often] come out in the same order you
put them in (they are "ordered" by the calculated, Hash values).

HTH,
Phill W.
 
H

HKSHK

Hello vbnewbie,
vbnewbie said:
Cor,

I have not joined this group to explain what I don't know.
I am here to get the advice from people loke those who have responded
to my posts and in fact have helped me make my app work.
If you think it's rubish, save yourself some time, don't respond.

Thanks

What Cor just meant was that there might be better responses if people
in the newsgroup understand what you want to accomplish rather than
just describing a little detail (there where you currently have a
problem). Sometimes it is better to see the whole picture rather than
just a small tessera.

Anyway, I'm happy to hear that your problem has been solved.

Best regards,

HKSHK
 
V

vbnewbie

Sorry to all, I guess I overeacted...
For beginners like me, it can sometimes be so overwhelming that it
becomes hard to find the words to explain what we are trying to do.
This said, some people seem to be better than others at understanding
us...
Thanks to all for you help, though, it is greatlty appreciated.
 

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