Hashtable

Discussion in 'ASP .Net' started by rk2008@gmail.com, Jun 14, 2007.

  1. Guest

    I am using Hashtable to keep Key-Value pair of elements.
    When I add the items to the Hashtable it does not retain the order in
    which I have added the key-value pair.
    Is there anyway to retain the order?
     
    , Jun 14, 2007
    #1
    1. Advertising

  2. wrote:
    > I am using Hashtable to keep Key-Value pair of elements.
    > When I add the items to the Hashtable it does not retain the order in
    > which I have added the key-value pair.
    > Is there anyway to retain the order?


    Not using a HashTable. You would have to also add them to a collection
    that does retain the order.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 14, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 14, 5:39 pm, Göran Andersson <> wrote:
    > wrote:
    > > I am usingHashtableto keep Key-Value pair of elements.
    > > When I add the items to theHashtableit does not retain the order in
    > > which I have added the key-value pair.
    > > Is there anyway to retain the order?

    >
    > Not using aHashTable. You would have to also add them to a collection
    > that does retain the order.
    >
    > --
    > Göran Andersson
    > _____http://www.guffa.com



    Is there a collection that retains the order?
     
    , Jun 14, 2007
    #3
  4. Howdy,

    Hastable does not retain the order because items are oragnised by the key
    hash code, allowing access in constant number of operations O(n). You would
    have to create an additional list containing keys:

    Hashtable hashtable =
    new Hashtable();

    List<string> history =
    new List<string>();

    for (int i = 0; i < 10; i++)
    {
    string key = Guid.NewGuid().ToString();

    hashtable.Add(key, Guid.NewGuid());
    history.Add(key);
    }

    // show how the items are organised in hashtable
    foreach (DictionaryEntry pair in hashtable)
    {
    System.Diagnostics.Debug.WriteLine(
    pair.Key.ToString() + "=" +
    pair.Value.ToString());
    }

    System.Diagnostics.Debug.WriteLine(String.Empty);

    // show items in order they were added
    foreach (string key in history)
    {
    System.Diagnostics.Debug.WriteLine(
    key + "=" +
    hashtable[key].ToString());
    }

    Hope this helps

    Milosz
    --
    Milosz


    "" wrote:

    > I am using Hashtable to keep Key-Value pair of elements.
    > When I add the items to the Hashtable it does not retain the order in
    > which I have added the key-value pair.
    > Is there anyway to retain the order?
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jun 15, 2007
    #4
  5. wrote:
    > On Jun 14, 5:39 pm, Göran Andersson <> wrote:
    >> wrote:
    >>> I am usingHashtableto keep Key-Value pair of elements.
    >>> When I add the items to theHashtableit does not retain the order in
    >>> which I have added the key-value pair.
    >>> Is there anyway to retain the order?

    >> Not using aHashTable. You would have to also add them to a collection
    >> that does retain the order.
    >>
    >> --
    >> Göran Andersson
    >> _____http://www.guffa.com

    >
    >
    > Is there a collection that retains the order?


    Most of them. I can only think of HashTable, Dictionary<> and
    SortedList<> that doesn't.

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 15, 2007
    #5
  6. The "Collection" object in the VB namespace does what you want, and there is
    no equivalent currently built into C# or the .NET Framework.
    But you can reference it and use it even if you're using C#.

    --
    I hope this helps,
    Steve C. Orr,
    MCSD, MVP, CSM, ASPInsider
    http://SteveOrr.net


    <> wrote in message
    news:...
    >I am using Hashtable to keep Key-Value pair of elements.
    > When I add the items to the Hashtable it does not retain the order in
    > which I have added the key-value pair.
    > Is there anyway to retain the order?
    >
     
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider], Jun 15, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guillermo

    Problem with hashTable

    Guillermo, Mar 4, 2004, in forum: Perl
    Replies:
    1
    Views:
    611
    Gunnar Hjalmarsson
    Mar 4, 2004
  2. Jonathan Wolfson

    vbc compilation fails when using Hashtable

    Jonathan Wolfson, Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    546
    Tu-Thach
    Jun 27, 2003
  3. John E

    Get Hashtable Object Directly

    John E, Oct 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,327
    Nicholas Paldino [.NET/C# MVP]
    Oct 8, 2003
  4. diya

    Type Hashtable not defined

    diya, Oct 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,776
    Nicole Calinoiu
    Oct 31, 2003
  5. D. Shane Fowlkes

    ArrayList versus HashTable

    D. Shane Fowlkes, Feb 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    17,786
    Kevin Spencer
    Feb 12, 2004
Loading...

Share This Page