i want to loop without the "for each" thru a hashtable..i know i've done this before..how do i do it?:confused:
Printable View
i want to loop without the "for each" thru a hashtable..i know i've done this before..how do i do it?:confused:
DUMB ANSWER ...censored by stupidity police :o
err you're not looping thru the hashtable you're just repeating the "VALUE" 2 times, what i mean by looping is itenerating by all the collection
and about my posts 95% of them are of posts asking things because i really know nothing!
I made an arse of it there...i was out late last night ;)
Apolgies
95% of my bum answers are because i dont listen...:oCode:Dim ht As New Hashtable()
ht.Add("KEY1", "VALUE1")
ht.Add("KEY2", "VALUE2")
Dim ie As IEnumerator = ht.GetEnumerator
While ie.MoveNext
Debug.WriteLine(CType(ie.Current, DictionaryEntry).Value)
End While
err just in case the "for each" is an automatic use of .GetEnumerator() , in fact you can only use for each in classes that support the IEnumerator..so i dont want to use it as it doesnt allow that some items get added when the loop is ocurring...i really must use a regular loop (that ones that in c# are for (int i = 0; i<something;i++) {} ) because otherwise some errors will be raised thru my code!
i asked in vb.net forums as more ppl look here than in c# forums lol but i understand vb.net sintax so there is no problem
Try anyone of these loops
Code:foreach(DictionaryEntry de in ht)
{
MessageBox.Show(de.Key.ToString() + " " + de.Value.ToString());
}
for(int i=0; i <= ht.Count; i++)
{
//do what you want here
}
devgrp i'd like to see the For(;;;) code and not the foreach..could u accomplish it? how do i refer to each item using the for(;;;) sintax?
I dont think you can with the for loop. The hashtable only supports an object indexer. You provide the key and it gives you the value, like this
I think you might have to use the foreach loop. I'll take another look though.Code:string name = ht["key"].ToString();
imagine i have a function that every 5 seconds lists all the the things of an hashtable to a listbox..and that sometimes i add 10 or 20 items to that hashtable..if when i am listing it is adding something to the hashtable an error will ocurr..for what i've reed up the only way to avoid it is to use a for(;;;) instead of a for each!
I'm not quite following. Write some pseudo code. That might help.Quote:
Originally posted by PT Exorcist
imagine i have a function that every 5 seconds lists all the the things of an hashtable to a listbox..and that sometimes i add 10 or 20 items to that hashtable..if when i am listing it is adding something to the hashtable an error will ocurr..for what i've reed up the only way to avoid it is to use a for(;;;) instead of a for each!
Is that because of some threading issue? Or else how can you be adding to it and looping in it at the sametime? If so I believe you can use SyncLock to avoid any trouble.
good idea! i thin synclock will do the job!
yep it seems to have worked! tks by the idea!