hashtable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vikas Kumar

    hashtable

    Hashtable 7/7/2006 2:37 AM
    I have this Hasttable windowList

    Hashtable windowList = new Hashtable();

    To this Hashtable I add a form with a string as key in the following manner.
    The string is decided at runtime.

    FormX frm = new FormX();
    frm.Show();
    windowList.add( "Form1",frm );

    Now I want to close all the Forms which i have added to this hashtable. How
    can i do this if i don't know the keys ( coz they were added at runtime )


  • Tom Spink

    #2
    Re: hashtable

    Vikas Kumar wrote:
    Hashtable 7/7/2006 2:37 AM
    I have this Hasttable windowList
    >
    Hashtable windowList = new Hashtable();
    >
    To this Hashtable I add a form with a string as key in the following
    manner. The string is decided at runtime.
    >
    FormX frm = new FormX();
    frm.Show();
    windowList.add( "Form1",frm );
    >
    Now I want to close all the Forms which i have added to this hashtable.
    How can i do this if i don't know the keys ( coz they were added at
    runtime )
    Hi Vikas,

    You can enumerate the keys, and close your windows like this:

    ///
    foreach ( string key in windowList.Keys )
    ((Form) windowList[key]).Close();
    ///

    --
    Hope this helps,
    Tom Spink

    Comment

    • Greg Young

      #3
      Re: hashtable

      Hashtable is enumerable or you could use the Keys property (but the
      enumerator will be faster)

      foreach(Diction aryEntry entry windowList) {
      ((Form) entry.Value)
      }

      Cheers,

      Greg Young
      MVP - C#


      "Vikas Kumar" <efextra@newsgr oups.nospamwrot e in message
      news:%23dev1tao GHA.3936@TK2MSF TNGP04.phx.gbl. ..
      Hashtable 7/7/2006 2:37 AM
      I have this Hasttable windowList
      >
      Hashtable windowList = new Hashtable();
      >
      To this Hashtable I add a form with a string as key in the following
      manner. The string is decided at runtime.
      >
      FormX frm = new FormX();
      frm.Show();
      windowList.add( "Form1",frm );
      >
      Now I want to close all the Forms which i have added to this hashtable.
      How can i do this if i don't know the keys ( coz they were added at
      runtime )
      >

      Comment

      • Mythran

        #4
        Re: hashtable


        "Greg Young" <druckdruckREMO VEgoose@hotmail .comwrote in message
        news:uUgZtxaoGH A.5084@TK2MSFTN GP03.phx.gbl...
        Hashtable is enumerable or you could use the Keys property (but the
        enumerator will be faster)
        >
        foreach(Diction aryEntry entry windowList) {
        ((Form) entry.Value)
        }
        >
        Cheers,
        >
        Greg Young
        MVP - C#

        >
        I agree that it may be faster to use the enumerator...bu t depending on what
        he is doing (closing windows) wouldn't it be safer and possibly more
        reliable to enumerate through the keys so the windows can be removed from
        the collection once closed? (Otherwise he/she/it would be attempting to
        remove items from the collection during the enumeration, which..umm, yeah,
        is kinda not right?)

        Thanks,
        Mythran

        Comment

        • Greg Young

          #5
          Re: hashtable

          Not really .. there are many ways to deal with having to remove things
          during an enumeration. One of the easiest to to work off of a clone which
          for small tables is also very cheap to build. There is also the question
          which needs to be asked of if you take a form out to close it and you fail,
          what do you do? Since this is closing all windows I would imagine that you
          have three possible cases, either barf out an error committing suicide, barf
          out an error and try to continue (likely in some sort of corrupted state),
          or just swallow the error and remove it anyways (atleast you are in a good
          state). My thoughts are that most people will either commit suicide or
          swallow the error and that relatively few will take the middle road.

          If you reflector the keys property you will see why I don't like the idea of
          using it much (especially its enumerator).

          Cheers,

          Greg
          "Mythran" <kip_potter@hot mail.comREMOVET RAILwrote in message
          news:Om77RqdoGH A.5104@TK2MSFTN GP04.phx.gbl...
          >
          "Greg Young" <druckdruckREMO VEgoose@hotmail .comwrote in message
          news:uUgZtxaoGH A.5084@TK2MSFTN GP03.phx.gbl...
          >Hashtable is enumerable or you could use the Keys property (but the
          >enumerator will be faster)
          >>
          >foreach(Dictio naryEntry entry windowList) {
          > ((Form) entry.Value)
          >}
          >>
          >Cheers,
          >>
          >Greg Young
          >MVP - C#
          >http://codebetter.com/blogs/gregyoung
          >>
          >
          I agree that it may be faster to use the enumerator...bu t depending on
          what he is doing (closing windows) wouldn't it be safer and possibly more
          reliable to enumerate through the keys so the windows can be removed from
          the collection once closed? (Otherwise he/she/it would be attempting to
          remove items from the collection during the enumeration, which..umm, yeah,
          is kinda not right?)
          >
          Thanks,
          Mythran
          >

          Comment

          • Bruce Wood

            #6
            Re: hashtable

            Mythran wrote:
            "Greg Young" <druckdruckREMO VEgoose@hotmail .comwrote in message
            news:uUgZtxaoGH A.5084@TK2MSFTN GP03.phx.gbl...
            Hashtable is enumerable or you could use the Keys property (but the
            enumerator will be faster)

            foreach(Diction aryEntry entry windowList) {
            ((Form) entry.Value)
            }

            Cheers,

            Greg Young
            MVP - C#
            >
            I agree that it may be faster to use the enumerator...bu t depending on what
            he is doing (closing windows) wouldn't it be safer and possibly more
            reliable to enumerate through the keys so the windows can be removed from
            the collection once closed? (Otherwise he/she/it would be attempting to
            remove items from the collection during the enumeration, which..umm, yeah,
            is kinda not right?)
            You would remove items from the collection as you go only if you're
            acting selectively on the items in the table. If you're closing every
            single window in the collection, you would just close them in a foreach
            loop and then nuke the contents of the entire collection in one go
            afterward. I think that this is the cleanest solution:

            foreach (Form f in windowList.Valu es)
            {
            f.Close();
            }
            windowList.Clea r();

            Since the f.Close() does not remove the form from the hashtable, the
            foreach will still work.

            Comment

            Working...