Results 1 to 13 of 13

Thread: how to loop thru an hashtable?

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    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/

  2. #2
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    DUMB ANSWER ...censored by stupidity police
    Last edited by powdir; Jun 21st, 2003 at 02:12 PM.

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  4. #4
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    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.

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  6. #6
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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
    }

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  9. #9

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    good idea! i thin synclock will do the job!
    \m/\m/

  13. #13

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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
  •  



Click Here to Expand Forum to Full Width