How can I get last item of a hashtable?

  • Thread starter Thread starter Crirus
  • Start date Start date
I don't think this is possible using the default HashTable. Although you
could implement your own version, inherited from the HashTable class that
does has this functionality.
 
Well, I add an object to a hashtable somewhere in a recursive function and
in another call I set some of that object properties
But I dont have a global value of the object, I only know that is the last
added to the HashTable
 
Crirus said:
Well, I add an object to a hashtable somewhere in a recursive
function and in another call I set some of that object properties
But I dont have a global value of the object, I only know that is the
last added to the HashTable

Can't you pass the object/the reference back to the calling procedure?
Alternatively, pass the key.
 
* "Crirus said:
Well, I add an object to a hashtable somewhere in a recursive function and
in another call I set some of that object properties
But I dont have a global value of the object, I only know that is the last
added to the HashTable

You will have to "remember" the object somewhere outside the hashtable,
for example in a private variable.
 
Crirus,
If knowing the last item added to a HashTable is important I would do as
Herfried suggested and "remember" what the last value added was.

If your HashTable is really a class that derives from DictionaryBase, I
would recommend overriding the OnInsertComplete method to save the last item
added to the custom dictionary. Then the Add method can use
Me.Dictionary.Add which will automatically call OnInsertComplete for you. If
you want the Add method to use Me.InnerHashTable.Add, you will need to set
the m_lastValue in the add routine also.

Public Class MyObjectCollection
Inherits Dicationary Base

Private m_lastValue As Object

Public Readonly Property LastValue() As Object
Get
Return m_lastValue
End Get
End Property

Public Sub Add(ByVal obj As MyObject)
Me.Dictionary.Add(obj.Key, obj)
End Sub

Protected Overrides Sub OnInsertComplete(ByVal key As Object, ByVal
value As Object)
m_lastValue = value
End Sub

End Class

If you have a plain HashTable variable You can simply override the Add
method in a class that you derive from HashTable and use this new class
instead of the regular HashTable.

Public Class HashTableEx
Inherits HashTable

Private m_lastValue As Object

' consider duplicating any constructors you need

Public Readonly Property LastValue() As Object
Get
Return m_lastValue
End Get
End Property

Public Overrides Sub Add(ByVal key As Object, _
ByVal value As Object)
MyBase.Add(key, value)
m_lastValue = value
End Sub

' optional, incase an item was added & then removed,
' before you had a chance to use it elsewhere.
Public Overrides Sub Remove(ByVal key As Object)
MyBase.Remove(key)
m_lastValue = Nothing
End Sub

End Class


Hope this helps
Jay


Crirus said:
Well, I add an object to a hashtable somewhere in a recursive function and
in another call I set some of that object properties
But I dont have a global value of the object, I only know that is the last
added to the HashTable
 
I inherited the Hashtable with a new field that store the reference to the
last object added. I overrided the Add method to set that field.. now is ok
and quite streightforward in order to minimize code changes
 
The second approach was exactly what I used only the name of the new class
is XP not Ex suffixed :)
And I dont have the remove

Thanks

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Jay B. Harlow said:
Crirus,
If knowing the last item added to a HashTable is important I would do as
Herfried suggested and "remember" what the last value added was.

If your HashTable is really a class that derives from DictionaryBase, I
would recommend overriding the OnInsertComplete method to save the last item
added to the custom dictionary. Then the Add method can use
Me.Dictionary.Add which will automatically call OnInsertComplete for you. If
you want the Add method to use Me.InnerHashTable.Add, you will need to set
the m_lastValue in the add routine also.

Public Class MyObjectCollection
Inherits Dicationary Base

Private m_lastValue As Object

Public Readonly Property LastValue() As Object
Get
Return m_lastValue
End Get
End Property

Public Sub Add(ByVal obj As MyObject)
Me.Dictionary.Add(obj.Key, obj)
End Sub

Protected Overrides Sub OnInsertComplete(ByVal key As Object, ByVal
value As Object)
m_lastValue = value
End Sub

End Class

If you have a plain HashTable variable You can simply override the Add
method in a class that you derive from HashTable and use this new class
instead of the regular HashTable.

Public Class HashTableEx
Inherits HashTable

Private m_lastValue As Object

' consider duplicating any constructors you need

Public Readonly Property LastValue() As Object
Get
Return m_lastValue
End Get
End Property

Public Overrides Sub Add(ByVal key As Object, _
ByVal value As Object)
MyBase.Add(key, value)
m_lastValue = value
End Sub

' optional, incase an item was added & then removed,
' before you had a chance to use it elsewhere.
Public Overrides Sub Remove(ByVal key As Object)
MyBase.Remove(key)
m_lastValue = Nothing
End Sub

End Class


Hope this helps
Jay
 
Crirus said:
I inherited the Hashtable with a new field that store the reference
to the last object added. I overrided the Add method to set that
field.. now is ok and quite streightforward in order to minimize code
changes

Good to know you found a solution.
 
I think is a good thing to post a found sollution if someone is
interested...:)
I followed some posts that lack on final solutions so I had to repost it or
ask for solution
 
Back
Top