|
-
Jun 21st, 2003, 10:15 AM
#1
Thread Starter
yay gay
how to loop thru an hashtable?
i want to loop without the "for each" thru a hashtable..i know i've done this before..how do i do it?
\m/  \m/
-
Jun 21st, 2003, 12:29 PM
#2
Addicted Member
DUMB ANSWER ...censored by stupidity police
Last edited by powdir; Jun 21st, 2003 at 02:12 PM.
-
Jun 21st, 2003, 01:01 PM
#3
Thread Starter
yay gay
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!
Last edited by PT Exorcist; Jun 21st, 2003 at 01:07 PM.
\m/  \m/
-
Jun 21st, 2003, 01:57 PM
#4
Addicted Member
I made an arse of it there...i was out late last night
Apolgies
Code:
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
95% of my bum answers are because i dont listen...
Last edited by powdir; Jun 21st, 2003 at 02:22 PM.
-
Jun 21st, 2003, 03:11 PM
#5
Thread Starter
yay gay
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
\m/  \m/
-
Jun 21st, 2003, 08:49 PM
#6
Frenzied Member
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
}
-
Jun 21st, 2003, 08:53 PM
#7
Thread Starter
yay gay
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?
\m/  \m/
-
Jun 21st, 2003, 09:21 PM
#8
Frenzied Member
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
Code:
string name = ht["key"].ToString();
I think you might have to use the foreach loop. I'll take another look though.
-
Jun 21st, 2003, 09:44 PM
#9
Thread Starter
yay gay
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!
\m/  \m/
-
Jun 21st, 2003, 11:58 PM
#10
Frenzied Member
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!
I'm not quite following. Write some pseudo code. That might help.
-
Jun 22nd, 2003, 02:20 AM
#11
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.
-
Jun 22nd, 2003, 07:11 AM
#12
Thread Starter
yay gay
good idea! i thin synclock will do the job!
\m/  \m/
-
Jun 22nd, 2003, 08:35 AM
#13
Thread Starter
yay gay
yep it seems to have worked! tks by the idea!
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|