Problem with Dictionary and Arraylist

M

Marek Zegarek

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
 
C

Chris Diver

Marek said:
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?
 
P

Phill W.

Marek said:
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.
 
P

Phillip Ross Taylor

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:
 
M

Marek Zegarek

Ok, everything clear/
You where right!
But I made one more thing - global declaration od arraylist.
Now works fine.

Thank!
 

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