473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

hashtable storage

Hi,

I have a hashtabel m_haschtbl = new Hashtable();
I need to get out my objects from my hashtable in the same order like they
were stored. the order is very important..

I have used IDictionaryEnumerator en = m_hasctbl.GetEnumerator();
while (en.MoveNext())
{
m_object = en.value;
// do some work here
}
is there any way to sort my objects???
thanks
is there
--
LZ
Jun 29 '06 #1
3 2643
Order of insertion is not retained in a hashtable.

I generally recommend people look at implementing a hashlist class in this
circumstance (build a linked list inside of your hash)

or you could just take a look at wintellect's power collections
http://www.wintellect.com/MemberOnly...llections.aspx

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Lamis" <La***@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
Hi,

I have a hashtabel m_haschtbl = new Hashtable();
I need to get out my objects from my hashtable in the same order like they
were stored. the order is very important..

I have used IDictionaryEnumerator en = m_hasctbl.GetEnumerator();
while (en.MoveNext())
{
m_object = en.value;
// do some work here
}
is there any way to sort my objects???
thanks
is there
--
LZ

Jun 29 '06 #2
As Greg indicated, here is an example of a "DictionaryHashList" that does
what you want:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;

namespace System.Collections.Specialized
{
public class DictionaryHashList : NameObjectCollectionBase
{
private DictionaryEntry _de = new DictionaryEntry();
// Creates an empty collection.
public DictionaryHashList()
{
}

// Adds elements from an IDictionary into the new collection.
public DictionaryHashList( IDictionary d, Boolean bReadOnly )
{
foreach ( DictionaryEntry de in d )
{
this.BaseAdd( (String) de.Key, de.Value );
}
this.IsReadOnly = bReadOnly;
}

// Gets a key-and-value pair (DictionaryEntry) using an index.
public DictionaryEntry this[ int index ]
{
get
{
_de.Key = this.BaseGetKey(index);
_de.Value = this.BaseGet(index);
return( _de );
}
}

// Gets or sets the value associated with the specified key.
public Object this[ String key ]
{
get
{
return( this.BaseGet( key ) );
}
set
{
this.BaseSet( key, value );
}
}

// Gets a String array that contains all the keys in the collection.
public String[] AllKeys
{
get
{
return( this.BaseGetAllKeys() );
}
}
// Gets an Object array that contains all the values in the collection.
public Array AllValues
{
get
{
return( this.BaseGetAllValues() );
}
}
// Gets a String array that contains all the values in the collection.
public String[] AllStringValues
{
get
{
return( (String[]) this.BaseGetAllValues( Type.GetType( "System.String"
) ) );
}
}

// Gets a value indicating if the collection contains keys that are not
null.
public Boolean HasKeys
{
get
{
return( this.BaseHasKeys() );
}
}

// Adds an entry to the collection.
public void Add( String key, Object value )
{
this.BaseAdd( key, value );
}

// Removes an entry with the specified key from the collection.
public void Remove( String key )
{
this.BaseRemove( key );
}

// Removes an entry in the specified index from the collection.
public void Remove( int index )
{
this.BaseRemoveAt( index );
}
// Clears all the elements in the collection.
public void Clear()
{
this.BaseClear();
}
}
}
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Lamis" wrote:
Hi,

I have a hashtabel m_haschtbl = new Hashtable();
I need to get out my objects from my hashtable in the same order like they
were stored. the order is very important..

I have used IDictionaryEnumerator en = m_hasctbl.GetEnumerator();
while (en.MoveNext())
{
m_object = en.value;
// do some work here
}
is there any way to sort my objects???
thanks
is there
--
LZ

Jun 29 '06 #3
Few ways to do but I will mention 2 possible ways here,

1. Sort it before insert
2. Cast it to SortedList using your objects and cast it back to HashTable.
chanmm

"Lamis" <La***@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
Hi,

I have a hashtabel m_haschtbl = new Hashtable();
I need to get out my objects from my hashtable in the same order like they
were stored. the order is very important..

I have used IDictionaryEnumerator en = m_hasctbl.GetEnumerator();
while (en.MoveNext())
{
m_object = en.value;
// do some work here
}
is there any way to sort my objects???
thanks
is there
--
LZ

Jun 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this?...
0
by: Ricardo Pereira | last post by:
Hi, I wrote a simple program to test the "out-of-the-box" functionalities of the CMAB (Configuration Management Application Block). After having added 2 values to an hashtable, and having written...
2
by: Alfonso Morra | last post by:
I am implementing an application which requires the storage of a large number of items in a cache. I have 3-tuple key reprented by a struct, as well as an n-tuple (n is fixed) dataset, also...
7
by: Naveen Mukkelli | last post by:
Hi, I'm working on a Client/Server application. Server application needs to keep track of all the clients. I'm planning to use a Hashtable for this purpose. There would be thousands of...
7
by: SevDer | last post by:
Hi We have a static hashtable that is located in another tier in our n-tiered web application. And we are storing big but not huge objects in this hashtable and the key to the objects is...
9
by: nithya4u | last post by:
Hi All, Currently, am working on converting some part of C# code to JAVA. Am having a problem with respect to the usage of Hashtable in C#. Is there a java equivalent for it? I have searched lots...
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
8
by: chrisben | last post by:
Hi, If I am not sure how many items will be in my Hashtable/ArrayList dynamically (multiple threads can add/remove/check the item), however, I can estimate the total number will not exceed...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.