PC Review


Reply
Thread Tools Rate Thread

How can I get last item of a hashtable?

 
 
Crirus
Guest
Posts: n/a
 
      7th Dec 2003
Easyest and fastest way...

--
Ceers,
Crirus

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

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


 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      7th Dec 2003
"Crirus" <(E-Mail Removed)> 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

 
Reply With Quote
 
Jan Tielens
Guest
Posts: n/a
 
      7th Dec 2003
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> Easyest and fastest way...
>
> --
> Ceers,
> Crirus
>
> ------------------------------
> If work were a good thing, the boss would take it all from you
>
> ------------------------------
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      7th Dec 2003
* "Crirus" <(E-Mail Removed)> 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>
 
Reply With Quote
 
Crirus
Guest
Posts: n/a
 
      7th Dec 2003
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]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "Crirus" <(E-Mail Removed)> 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>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      7th Dec 2003
"Crirus" <(E-Mail Removed)> 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

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      7th Dec 2003
* "Crirus" <(E-Mail Removed)> 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>
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      7th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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]" <hirf-spam-me-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > * "Crirus" <(E-Mail Removed)> 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>

>
>



 
Reply With Quote
 
Crirus
Guest
Posts: n/a
 
      8th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Crirus" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
Crirus
Guest
Posts: n/a
 
      8th Dec 2003
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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]" <hirf-spam-me-(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > * "Crirus" <(E-Mail Removed)> 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>

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashtable.Item Peter Kirk Microsoft C# .NET 2 10th Oct 2005 03:33 PM
Remove Item from hashtable MFRASER Microsoft C# .NET 2 17th Nov 2004 11:33 PM
Hashtable Item in C# Anony Microsoft C# .NET 2 10th Jul 2004 12:35 PM
Last item of a Hashtable Isaias Formacio Serna Microsoft Dot NET Compact Framework 6 22nd Jan 2004 03:21 PM
Hashtable.Item property Andrew Chen Microsoft C# .NET 1 10th Jul 2003 01:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:29 PM.