473,789 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2970
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("Test 1")

>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.goo glegroups.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.Ad d(_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******** ************@s3 4g2000cwa.googl egroups.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.goog legroups.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).Coun t

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

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

Similar topics

1
6472
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 my WebMethod I Initialize my project as following:
8
5044
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 change the value in that method the modification will not be visible outside the method. However this is not true for classes. In my example I am not using ref keyword. Thanks for feedback.
2
1247
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 within the IDE debug symbols for system.dll, drawing.dll, forms.dll to trace this error? -- Steve
6
2849
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 types hold a reference to an object as opposed to object data itself. So, when reference type parameter is passed into a method, a copy of objects reference is passed in, so called method can do whatever to "original" object and a caller will see...
6
1419
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 believes that the file is still in use by the application. My concern is that I do not think that I still have a reference to it. In the removeImage function, I remove the item from the image list and list view controls and refresh the list view control to...
7
6787
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 = LoadOracleData(sql) '____Do amazing things
2
2707
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 object error" i can't seem to see what's wrong with my code so i'm hoping some fresh eyes can spot what's wrong or what i'm missing. the following is the code, this line strFile = CStr(xlSheet.Range("I" + CStr(Row)).Value.ToString + "\" +...
2
1343
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 as far as finding open windows, and detecting which ones are internet explorer windows, but I do not know how to store the ones that are internet explorer windows and reference them later. My idea was that I could create an array of type...
4
1766
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 the COM object will never get release, Even we call gc.Collect() But the same code compiles in .NET 2.0 works without the Marshal.ReleaseComObject. (Unfortunately the project is supposed to run on .NET 1.1.)
11
4674
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 cli:array<cli::interior_ptr<float>, 2>... So, what do I do?
0
9665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.