471,066 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How can I get last item of a hashtable?

Easyest and fastest way... :)

--
Ceers,
Crirus

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

------------------------------
Nov 20 '05 #1
11 2428
"Crirus" <Cr****@datagroup.ro> schrieb
Easyest and fastest way... :)


Do the items in a hashtable have a defined order?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
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.

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Crirus" <Cr****@datagroup.ro> schreef in bericht
news:uQ**************@TK2MSFTNGP11.phx.gbl...
Easyest and fastest way... :)

--
Ceers,
Crirus

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

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

Nov 20 '05 #3
* "Crirus" <Cr****@datagroup.ro> scripsit:
Easyest and fastest way... :)


Why? The order ot the items in a hashtable does not matter.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
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
--
Ceers,
Crirus

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

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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
* "Crirus" <Cr****@datagroup.ro> scripsit:
Easyest and fastest way... :)


Why? The order ot the items in a hashtable does not matter.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #5
"Crirus" <Cr****@datagroup.ro> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
* "Crirus" <Cr****@datagroup.ro> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7
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" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
--
Ceers,
Crirus

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

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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
* "Crirus" <Cr****@datagroup.ro> scripsit:
Easyest and fastest way... :)


Why? The order ot the items in a hashtable does not matter.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


Nov 20 '05 #8
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

--
Ceers,
Crirus

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

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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
"Crirus" <Cr****@datagroup.ro> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OI**************@TK2MSFTNGP09.phx.gbl...
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" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
--
Ceers,
Crirus

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

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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
* "Crirus" <Cr****@datagroup.ro> scripsit:
> Easyest and fastest way... :)

Why? The order ot the items in a hashtable does not matter.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>



Nov 20 '05 #10
"Crirus" <Cr****@datagroup.ro> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #11
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

--
Cheers,
Crirus

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

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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:eh***************@TK2MSFTNGP10.phx.gbl...
"Crirus" <Cr****@datagroup.ro> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andrew Chen | last post: by
1 post views Thread by Thomas Wang | last post: by
2 posts views Thread by Anony | last post: by
2 posts views Thread by MFRASER | last post: by
2 posts views Thread by Peter Kirk | last post: by
2 posts views Thread by Shen | last post: by
reply views Thread by leo001 | last post: by

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.