Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Hashtable

Reply
Thread Tools

Hashtable

 
 
rk2008@gmail.com
Guest
Posts: n/a
 
      06-14-2007
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?

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      06-14-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
rk2008@gmail.com
Guest
Posts: n/a
 
      06-14-2007
On Jun 14, 5:39 pm, Göran Andersson <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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?

 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      06-14-2007
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


"(E-Mail Removed)" 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?
>
>

 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      06-14-2007
(E-Mail Removed) wrote:
> On Jun 14, 5:39 pm, Göran Andersson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
Guest
Posts: n/a
 
      06-15-2007
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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?
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: hashtable or map? (map inserts not behaving as I expect - and I cant find a decent simple example for hashtable) Kai-Uwe Bux C++ 1 12-21-2008 09:25 PM
Problem with hashTable Guillermo Perl 1 03-04-2004 12:43 PM
Type Hashtable not defined diya ASP .Net 1 10-31-2003 06:59 PM
Get Hashtable Object Directly John E ASP .Net 1 10-08-2003 12:40 PM
vbc compilation fails when using Hashtable Jonathan Wolfson ASP .Net 1 06-27-2003 04:40 PM



Advertisments