Hi,
I am picking up C#.net and I'm trying to add many values to one single
key at different time in a loop.
If the key already exist, append new value to previous?
I'm not sure how to do that with ArrayList, any help? Thanks.
Hashtable data = new Hashtable();
if (!data.Contains(key))
{
data.Add(key, value);
}
else
{
//previous value already added
//want to add new value & keep previous value as well
} 6 3405
On Wed, 01 Oct 2008 17:03:16 -0700, Slickuser <sl*********@gmail.com>
wrote:
Hi,
I am picking up C#.net and I'm trying to add many values to one single
key at different time in a loop.
If the key already exist, append new value to previous?
I'm not sure how to do that with ArrayList, any help? Thanks.
Simply retrieve the ArrayList instance associated with the key, and add
your new value to the list. You only need to modify the ArrayList itself;
the Hashtable already has the reference to that instance of ArrayList for
that key, and so won't need to be updated in that scenario.
Note that you may find the List<Tclass more appropriate. ArrayList was
fine pre-generics, but List<Toffers a type-safe equivalent.
Pete
On Oct 1, 5:32*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 01 Oct 2008 17:03:16 -0700, Slickuser <slick.us...@gmail.com*
wrote:
Hi,
I am picking up C#.net and I'm trying to add many values to one single
key at different time in a loop.
If the key already exist, append new value to previous?
I'm not sure how to do that with ArrayList, any help? Thanks.
Simply retrieve the ArrayList instance associated with the key, and add *
your new value to the list. *You only need to modify the ArrayList itself; *
the Hashtable already has the reference to that instance of ArrayList for*
that key, and so won't need to be updated in that scenario.
Can I see that in code example? Thanks.
>
Note that you may find the List<Tclass more appropriate. *ArrayList was *
fine pre-generics, but List<Toffers a type-safe equivalent.
Pete
On Wed, 01 Oct 2008 17:37:59 -0700, Slickuser <sl*********@gmail.com>
wrote:
>Simply retrieve the ArrayList instance associated with the key, and add your new value to the list. [...]
Can I see that in code example? Thanks.
What part do you need help with? You simply need to make sure you're
storing an ArrayList instance as the value for the Hashtable rather than
the actual value. Then just keep adding new values to the ArrayList as
necessary.
The code, at its most basic, looks something like this:
if (!data.Contains(key))
{
ArrayList list = new ArrayList();
list.Add(value);
data[key] = list;
}
else
{
data[key].Add(value);
}
Again, I recommend the generic classes. List<Tand Dictionary<TKey,
TValuewould be much better than the older Hashtable and ArrayList.
Pete
Thanks a lot.
On Oct 1, 5:55*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 01 Oct 2008 17:37:59 -0700, Slickuser <slick.us...@gmail.com*
wrote:
Simply retrieve the ArrayList instance associated with the key, and add *
your new value to the list. [...]
Can I see that in code example? Thanks.
What part do you need help with? *You simply need to make sure you're *
storing an ArrayList instance as the value for the Hashtable rather than *
the actual value. *Then just keep adding new values to the ArrayList as*
necessary.
The code, at its most basic, looks something like this:
* * * * *if (!data.Contains(key))
* * * * *{
* * * * * * *ArrayList list = new ArrayList();
* * * * * * *list.Add(value);
* * * * * * *data[key] = list;
* * * * *}
* * * * *else
* * * * *{
* * * * * * *data[key].Add(value);
data.Add(key, value);
* * * * *}
Again, I recommend the generic classes. *List<Tand Dictionary<TKey, *
TValuewould be much better than the older Hashtable and ArrayList.
Pete
On Oct 1, 5:55*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 01 Oct 2008 17:37:59 -0700, Slickuser <slick.us...@gmail.com*
wrote:
Simply retrieve the ArrayList instance associated with the key, and add *
your new value to the list. [...]
Can I see that in code example? Thanks.
What part do you need help with? *You simply need to make sure you're *
storing an ArrayList instance as the value for the Hashtable rather than *
the actual value. *Then just keep adding new values to the ArrayList as*
necessary.
The code, at its most basic, looks something like this:
* * * * *if (!data.Contains(key))
* * * * *{
* * * * * * *ArrayList list = new ArrayList();
* * * * * * *list.Add(value);
* * * * * * *data[key] = list;
data.Add(key, value);
* * * * *}
* * * * *else
* * * * *{
* * * * * * *data[key].Add(value);
ArrayList list = new ArrayList();
list.Add(value);
data[key] = list;
//Doesn't this clear my previous values for that specific
key if I am doing in a while loop?
* * * * *}
Again, I recommend the generic classes. *List<Tand Dictionary<TKey, *
TValuewould be much better than the older Hashtable and ArrayList.
Pete
On Wed, 01 Oct 2008 18:32:36 -0700, Slickuser <sl*********@gmail.com>
wrote:
ArrayList list = new ArrayList();
list.Add(value);
data[key] = list;
//Doesn't this clear my previous values for that specific
key if I am doing in a while loop?
No. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JezB |
last post by:
How can I loop thru a hashtable changing the Value of each entry ? Whatever
I try I always seem to get the error about modifying the collection within
the loop.
|
by: Dan |
last post by:
I have the need to translate values back and forth. I have created a
Hashtable to do this because I don't care about intrinsic order and I
want it to be a fast as possible. My only concern is...
|
by: Fred |
last post by:
I'm trying to build a hashtable and a arraylist as object value
I'm not able to retrieve stored object from the hashtable.
Hashtable mp = new Hashtable(); // THE HASHTABLE
ArrayList...
|
by: SK |
last post by:
Hey,
i have a hashtable, where I am adding some values. Now
when I iterate through them then they start in reverse
order, why is that happening and how can I get rid of it?
Thanks
|
by: J L |
last post by:
I have defined a structure
private structure FieldInfo
dim FieldName as string
dim OrdinalPostioin as Integer
dim DataType as Type
dim Size as Integer
end structure
I read this information...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |