hashtable storage

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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();
}
}
}
 
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top