473,386 Members | 1,679 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,386 software developers and data experts.

Problem with Dictionary and Arraylist

Hello!

I'm trying to write a simple sub, but somewhere I'm making a mistake.....
Problem is somewhere in adding elements to arraylist or dictionary....

I need to add in Dictionary one string and collection of elements as
parameter.
Then I need to add next record to Dictionary with other string and other
collection.
Can someone correct this sample code?

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim arrElem As New ArrayList
Dim I As Integer = 0
Dim collect As String() = {1, 2, 3, 4, 5}

For Each element As String In collect
arrElem.Add(element)
arrElem.Add(element + 2)
dictAll.Add("Wiersz " & I, arrElem)
I += 1
Next
Jun 14 '07 #1
4 2660
Marek Zegarek wrote:
Hello!

I'm trying to write a simple sub, but somewhere I'm making a
mistake..... Problem is somewhere in adding elements to arraylist or
dictionary....

I need to add in Dictionary one string and collection of elements as
parameter.
Then I need to add next record to Dictionary with other string and other
collection.
Can someone correct this sample code?

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim arrElem As New ArrayList
Dim I As Integer = 0
Dim collect As String() = {1, 2, 3, 4, 5}

For Each element As String In collect
arrElem.Add(element)
arrElem.Add(element + 2)
dictAll.Add("Wiersz " & I, arrElem)
I += 1
Next

What's the error message? What line is it on?
Jun 14 '07 #2
Marek Zegarek wrote:
Hello!

I'm trying to write a simple sub, but somewhere I'm making a
mistake..... Problem is somewhere in adding elements to arraylist or
dictionary....

I need to add in Dictionary one string and collection of elements as
parameter.
Then I need to add next record to Dictionary with other string and other
collection.
Can someone correct this sample code?

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim arrElem As New ArrayList
Dim I As Integer = 0
Dim collect As String() = {1, 2, 3, 4, 5}

For Each element As String In collect
arrElem.Add(element)
arrElem.Add(element + 2)
dictAll.Add("Wiersz " & I, arrElem)
I += 1
Next
I'm guessing that all your ArrayLists seem to have the same set of
values in them.
That's because you're extending the /same/ ArrayList object every time
and adding it into your Dictionary repeatedly.

The following might be a little better.

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim collect As String() = {1, 2, 3, 4, 5}

For I as Integer = 0 To collect.Length - 1
Dim arrElem As New ArrayList
arrElem.Add( collect( I ) )
arrElem.Add( collect( I ) & 2 )
dictAll.Add( "Wiersz " & I.ToString(), arrElem )
Next
HTH,
Phill W.
Jun 14 '07 #3
On 14 Jun, 09:38, "Marek Zegarek" <Marek.zega...@gov.plwrote:
Hello!

I'm trying to write a simple sub, but somewhere I'm making a mistake.....
Problem is somewhere in adding elements to arraylist or dictionary....

I need to add in Dictionary one string and collection of elements as
parameter.
Then I need to add next record to Dictionary with other string and other
collection.
Can someone correct this sample code?

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim arrElem As New ArrayList
Dim I As Integer = 0
Dim collect As String() = {1, 2, 3, 4, 5}

For Each element As String In collect
arrElem.Add(element)
arrElem.Add(element + 2)
dictAll.Add("Wiersz " & I, arrElem)
I += 1
Next
There is no problem with this code.

What's wierd is you create a variable called collect which is an array
of strings and then this is used ONLY to control the number of times
the loop happens.

This would be better and easier to understand:
Dim dictAll As New Dictionary(Of String, ArrayList)
Dim arrElem As New ArrayList

For i as int = 0 to 4
arrElem.Add(element)
arrElem.Add(element + 2)
dictAll.Add("Wiersz " & i, arrElem)
Next
Jun 14 '07 #4
Ok, everything clear/
You where right!
But I made one more thing - global declaration od arraylist.
Now works fine.

Thank!

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:f4**********@south.jnrs.ja.net...
Marek Zegarek wrote:
>Hello!

I'm trying to write a simple sub, but somewhere I'm making a mistake.....
Problem is somewhere in adding elements to arraylist or dictionary....

I need to add in Dictionary one string and collection of elements as
parameter.
Then I need to add next record to Dictionary with other string and other
collection.
Can someone correct this sample code?

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim arrElem As New ArrayList
Dim I As Integer = 0
Dim collect As String() = {1, 2, 3, 4, 5}

For Each element As String In collect
arrElem.Add(element)
arrElem.Add(element + 2)
dictAll.Add("Wiersz " & I, arrElem)
I += 1
Next

I'm guessing that all your ArrayLists seem to have the same set of values
in them.
That's because you're extending the /same/ ArrayList object every time and
adding it into your Dictionary repeatedly.

The following might be a little better.

Dim dictAll As New Dictionary(Of String, ArrayList)
Dim collect As String() = {1, 2, 3, 4, 5}

For I as Integer = 0 To collect.Length - 1
Dim arrElem As New ArrayList
arrElem.Add( collect( I ) )
arrElem.Add( collect( I ) & 2 )
dictAll.Add( "Wiersz " & I.ToString(), arrElem )
Next
HTH,
Phill W.
Jun 14 '07 #5

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

Similar topics

6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
12
by: teoryn | last post by:
I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code: *--beginning of file--*...
1
by: Adnan | last post by:
Hi, I am porting an existing ASP applcation to ASP.NET which heavily depends on Mainpulation through Scripting.Dictionary. Can anyone help me out with an alternative to use of Scripting.Dictionary....
4
by: Art | last post by:
Hi, Class1 will be instantiated for each "region". Class2 will do the instantiation as it reads through records in a database. In Class2 I need to create and use the information in an instance...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
15
by: GTi | last post by:
If I use: ArrayList TimeScale = new ArrayList(); TimeScale.Capacity = 1000; TimeScale="test 1" The last line trow me an error: Index was out of range. Must be non-negative and less than the...
3
by: shrishjain | last post by:
Hi All, I have to make multiple dictionary objects(Dictionary<string key, int value>) with same set of string keys, and that really eats up lot of space. I am trying to find a solution where I...
20
by: Gustaf | last post by:
This is two questions in one really. First, I wonder how to convert the values in a Dictionary to an array. Here's the dictionary: private Dictionary<Uri, Schemaschemas = new Dictionary<Uri,...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
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,...
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...

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.