473,402 Members | 2,046 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,402 software developers and data experts.

How to create and reference an Arraylist at run-time

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!

Jan 3 '07 #1
16 2939
This is for Visual Basic 2005
vbnewbie wrote:
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!
Jan 3 '07 #2
Hello vbnewbie,
vbnewbie wrote:
>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
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")

>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)
You could e.g. define that the first entry in the ArrayList always
contains the name.
>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)
In this case I would make them public in a module.

Best Regards,

HKSHK
Jan 3 '07 #3

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?

Jan 3 '07 #4
Yes. A Module.

Public MyArrayLists As New ArrayList

MyArrayLists will be available from anywhere in your project.
"vbnewbie" <bi*****@gmail.comwrote in message
news:11**********************@q40g2000cwq.googlegr oups.com...
>
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?

Jan 4 '07 #5

Stephany Young wrote:
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...

Jan 4 '07 #6
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.
"vbnewbie" <bi*****@gmail.comwrote in message
news:11********************@s34g2000cwa.googlegrou ps.com...
>
Stephany Young wrote:
>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...

Jan 4 '07 #7
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

"vbnewbie" <bi*****@gmail.comschreef in bericht
news:11**********************@11g2000cwr.googlegro ups.com...
>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!

Jan 4 '07 #8
vbnewbie wrote:
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.
Jan 4 '07 #9

Cor Ligthert [MVP] wrote:
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

Jan 4 '07 #10
Stephany Young wrote:
>
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!

Jan 4 '07 #11
Stephany Young wrote:
>
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!

Jan 4 '07 #12

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

Jan 4 '07 #13
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
Jan 5 '07 #14
vbnewbie wrote:
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.
Jan 5 '07 #15
Hello vbnewbie,
vbnewbie schrieb:
Cor Ligthert [MVP] wrote:
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
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

Jan 5 '07 #16
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.

HKSHK wrote:
Hello vbnewbie,
vbnewbie schrieb:
Cor Ligthert [MVP] wrote:
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

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
Jan 5 '07 #17

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

Similar topics

1
by: dotnetnewbie | last post by:
Hi all I am new to .NET and webservice so I wish someone can shed some light on me. I have a Project class and a Product class, the Project can contain multiple Products (as an ArrayList). In...
8
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then...
2
by: Steve Teeples | last post by:
I keep getting the error "Object reference not set to an instance of an object." The unhandled exception is a System.NullReferenceException. It is occurring in an "unknown module". How do I load...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
6
by: Stephen Costanzo | last post by:
In the code below I receive an exception error because the target file is being used by another process. I know this to be false because the target doesn't exist. It seems like the application...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
2
by: dotnetnoob | last post by:
i got this program that will fetch the data in the excel spreadsheet, it was working before then i make some adjustment and it now give me an error of "Object reference not set to an instance of an...
2
by: garyusenet | last post by:
I am writing a programme that when run will list the current internet explorer windows open, and allow the user to choose which one they would like to work with. At the moment I have managed to get...
4
by: johnxhc | last post by:
We have a project in .NET 1.1 , some of the .NET methods take COM interface reference pointer as a parameter, somehow we have to call Marshal.ReleaseComObject to release the COM Object, otherwise...
11
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.