Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Hashtable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rk2008@gmail.com - 14 Jun 2007 22:31 GMT
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?
Göran Andersson - 14 Jun 2007 22:39 GMT
> 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.

Signature

Göran Andersson
_____
http://www.guffa.com

rk2008@gmail.com - 14 Jun 2007 22:53 GMT
On Jun 14, 5:39 pm, G?ran Andersson <g...@guffa.com> wrote:
> rk2...@gmail.com wrote:
> > I am usingHashtableto keep Key-Value pair of elements.
[quoted text clipped - 8 lines]
> G?ran Andersson
> _____http://www.guffa.com

Is there a collection that retains the order?
Göran Andersson - 15 Jun 2007 00:39 GMT
>> rk2...@gmail.com wrote:
>>> I am usingHashtableto keep Key-Value pair of elements.
[quoted text clipped - 9 lines]
>
> Is there a collection that retains the order?

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

Signature

Göran Andersson
_____
http://www.guffa.com

Milosz Skalecki [MCAD] - 15 Jun 2007 00:16 GMT
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
Signature

Milosz

> 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] - 15 Jun 2007 03:38 GMT
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#.

Signature

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

>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?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.