C
Crirus
Easyest and fastest way...
Crirus said:Easyest and fastest way...
* "Crirus said:Easyest and fastest way...
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
* "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
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
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