Last item of a Hashtable

  • Thread starter Thread starter Isaias Formacio Serna
  • Start date Start date
I

Isaias Formacio Serna

Hi, is there a way to get the last inserted item of a hashtable in the
compact framework?? What would you recomend??

Thanks.

Isaias Formacio
 
there is probably on.
but you could always try the value or key enumerator.

it could be that for either the keys or values, they would be enumerate in
the order of insertion or the reverse...
 
The problem with this is that it will rely on the internal implementation
which can change at any time.
The clean way to do this is to keep something like an arraylist of keys and
update it simultaneously, preferrably wrapping this into a new class.
Or, if you only care about the last item, derive a class from the hashtable
and override all methods that add items (in fact just override Insert()). In
you override store the key value in some member variable.
 
Maybe a plain old collection would work too.
If you always add items after the collection's .count then they would be in
the order added and you could get at the last one, or any other one by using
the key.
 
There is no (safe) way. You could extend the hashtable class and when you
insert a key/value, store a reference to it. Then add a LastInsertedKey()
method that simply does a "return this.lastKey;"

Hilton
 
Thanks, I think this would be the best solution, to wrap the Hashtable into
then new class, override the Add function and create a property that points
to the last inserted object.

Thanks to you all for your suggestions.

Isaias Formacio Serna
 
This was it:

public class HashTable : System.Collections.Hashtable

{

private object _last;

public object Last

{

get

{

return this._last;

}

}

public override void Add(object key, object value)

{

this._last = value;

base.Add (key, value);

}

}

Of course it uses my application's namespace so I can make a difference with
the System.Collections.Hashtable.
 
Back
Top