473,666 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ele ment)
arrElem.Add(ele ment + 2)
dictAll.Add("Wi ersz " & I, arrElem)
I += 1
Next
Jun 14 '07 #1
4 2670
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(ele ment)
arrElem.Add(ele ment + 2)
dictAll.Add("Wi ersz " & 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(ele ment)
arrElem.Add(ele ment + 2)
dictAll.Add("Wi ersz " & 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(ele ment)
arrElem.Add(ele ment + 2)
dictAll.Add("Wi ersz " & 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(ele ment)
arrElem.Add(ele ment + 2)
dictAll.Add("Wi ersz " & 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.j a.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(ele ment)
arrElem.Add(ele ment + 2)
dictAll.Add("Wi ersz " & 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
16983
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 through a dictionary, looking for things, then remove them.... John
12
2185
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--* #!/usr/bin/python # Filename: unscram.py def sort_string(word): '''Returns word in lowercase sorted alphabetically'''
1
3172
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. Secondly, most of the migration done through VS.NET wizard has placed the existing ASP code in <Script> tags in an aspx place. Does the runtime take <script language="VB" runat="Server"> tag as a .NET code and compile it or leaves it for...
4
1846
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 of Class1. As I read a record with a new "region" I need to instantiate a new Class1. If I read a record with a "region" I've already seen, I'll need to update the appropriate member of Class1.
1
9249
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 DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
15
1698
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 size of the collection. Parameter name: index
3
11389
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 can save space- In my current implementation all dictionary objects store the same keys repetitively and that eats up lot of space. Does .net framework comes to help in such situation? The best I can think of is having a separate dictionary...
20
34139
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, Schema>(); Uri is the System.Uri class, and Schema is a class I made. Now, I want the class where this Dictionary is contained to have a property that returns all the values in this Dictionary. Like such: public Schema Schemas
9
3107
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 to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
0
8444
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
8869
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
8781
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...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1775
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.