*URGENT Comparing objects stored in hashtable/collection

G

Guest

Hi! I need to compare objects that I store in HashTable by using Add method
and passing object as first param, and a unique key string as second. Later
on I iterate through this collection and need to compare the object stored in
the HashTable to another object that's obtained as one of the params in a
call to an event.
Example code (i'm skipping most declarations to avoid long post):
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Dim fsw as FileSystemWatcher
fsw = source
myEnumerator = hshtable.GetEnumerator()
While myEnumerator.MoveNext

if myEnumerator.Value Is fsw then
'do something if the object is the same
end if

End While

I also tried using myEnumerator.Current when performing comparison, as well
as trying to cast the .Value and .Current to the same type of object that I
compare it to (FileSystemWatcher). I'm open to any suggestions. The basic
goal of what I'm doing is to store a reference to an object and an associated
string somewhere so that I can later when the event of FileSystemObject fires
identify that object and associated string to perform some relevant action.
I have a pretty short Deadline on this so any help would be appreciated!

Thanks!
 
J

Jeff Gaines

Hi! I need to compare objects that I store in HashTable by using Add
method and passing object as first param, and a unique key string as
second. Later on I iterate through this collection and need to
compare the object stored in the HashTable to another object that's
obtained as one of the params in a call to an event.
Example code (i'm skipping most declarations to avoid long post):
[snip]


I also tried using myEnumerator.Current when performing comparison,
as well as trying to cast the .Value and .Current to the same type of
object that I compare it to (FileSystemWatcher). I'm open to any
suggestions. The basic goal of what I'm doing is to store a
reference to an object and an associated string somewhere so that I
can later when the event of FileSystemObject fires identify that
object and associated string to perform some relevant action. I have
a pretty short Deadline on this so any help would be appreciated!

Thanks!

Can you cast objects in VB.NET?

In C# I would use:

// Get corresponding TreeNode object
TreeNode tn = (TreeNode)hashTable[currentHandle];

Where the HashTable contains pairs of TreeNodes/TreeNode handles.

There must be something similaer in VB, perhaps
hashTable[currentHandle] as TreeNode?
 
G

Guest

Thanks for suggestion Jeff, but as I mentioned in my initial post I have
tried that approach. I might have gone wrong about doing though, so any
suggestions as to how to do it the right way would be great. VB.net has
CType function that allows you to do the casting/converting between types, so
I tried using it by saying
CType(ojbect1, FileSystemWatcher) and then comparing it to the source object
that already suppose to be of that type. I've also tried casting the source
object to the same type, but VB throws exceptions not liking one of those
particualar conversions.

Jeff Gaines said:
Hi! I need to compare objects that I store in HashTable by using Add
method and passing object as first param, and a unique key string as
second. Later on I iterate through this collection and need to
compare the object stored in the HashTable to another object that's
obtained as one of the params in a call to an event.
Example code (i'm skipping most declarations to avoid long post):
[snip]


I also tried using myEnumerator.Current when performing comparison,
as well as trying to cast the .Value and .Current to the same type of
object that I compare it to (FileSystemWatcher). I'm open to any
suggestions. The basic goal of what I'm doing is to store a
reference to an object and an associated string somewhere so that I
can later when the event of FileSystemObject fires identify that
object and associated string to perform some relevant action. I have
a pretty short Deadline on this so any help would be appreciated!

Thanks!

Can you cast objects in VB.NET?

In C# I would use:

// Get corresponding TreeNode object
TreeNode tn = (TreeNode)hashTable[currentHandle];

Where the HashTable contains pairs of TreeNodes/TreeNode handles.

There must be something similaer in VB, perhaps
hashTable[currentHandle] as TreeNode?
 
©

©tefan ©imek

What exactly is in the hashtable?

If you want to map a string to the FileSystemWatcher, then you should use
the fsw's as keys and strings as values. And if you are doing so, simply
casting the return value of the hshtable[fsw] call to string should be
enough. So I guess your code should be:

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Dim fsw as FileSystemWatcher
Dim val as String
fsw = source
val = hshtable[fsw]

'if the val isn't Nothing, you've got what you need.

HTH,
Stefan

Alex said:
Thanks for suggestion Jeff, but as I mentioned in my initial post I have
tried that approach. I might have gone wrong about doing though, so any
suggestions as to how to do it the right way would be great. VB.net has
CType function that allows you to do the casting/converting between types,
so
I tried using it by saying
CType(ojbect1, FileSystemWatcher) and then comparing it to the source
object
that already suppose to be of that type. I've also tried casting the
source
object to the same type, but VB throws exceptions not liking one of those
particualar conversions.

Jeff Gaines said:
Hi! I need to compare objects that I store in HashTable by using Add
method and passing object as first param, and a unique key string as
second. Later on I iterate through this collection and need to
compare the object stored in the HashTable to another object that's
obtained as one of the params in a call to an event.
Example code (i'm skipping most declarations to avoid long post):
[snip]


I also tried using myEnumerator.Current when performing comparison,
as well as trying to cast the .Value and .Current to the same type of
object that I compare it to (FileSystemWatcher). I'm open to any
suggestions. The basic goal of what I'm doing is to store a
reference to an object and an associated string somewhere so that I
can later when the event of FileSystemObject fires identify that
object and associated string to perform some relevant action. I have
a pretty short Deadline on this so any help would be appreciated!

Thanks!

Can you cast objects in VB.NET?

In C# I would use:

// Get corresponding TreeNode object
TreeNode tn = (TreeNode)hashTable[currentHandle];

Where the HashTable contains pairs of TreeNodes/TreeNode handles.

There must be something similaer in VB, perhaps
hashTable[currentHandle] as TreeNode?
 
G

Guest

Stefan, I don't think you fully understand what I'm doing thus far and why I
am having the problem. Hashtable (in case you don't know) is a collection of
sorts where you can store associated pairs of objects(anything, strings,
numbers, objects,etc...) and keys (strings) that identify them. You can then
use various methods of HashTable class to walk through the list of items you
have stored in it and return the objects you stored and the associated keys,
or use the key to locate a specific object.
My problem is that I cannot use the key to search for the object I need, I
need to compare the object stored in the HashTable to the object that's being
passed to the OnChanged event of the FileWatcher that fires at a particular
time. Once I am able to establish which specific object stored in the
HashTable is the one that's currently reaising it's event, I can get the
associated Key that I stored in HashTable to perform further actions in the
database. I hope you or anyone else will have some suggestions either to
improve this method or to do it some other way to achieve end result.

©tefan ©imek said:
What exactly is in the hashtable?

If you want to map a string to the FileSystemWatcher, then you should use
the fsw's as keys and strings as values. And if you are doing so, simply
casting the return value of the hshtable[fsw] call to string should be
enough. So I guess your code should be:

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Dim fsw as FileSystemWatcher
Dim val as String
fsw = source
val = hshtable[fsw]

'if the val isn't Nothing, you've got what you need.

HTH,
Stefan

Alex said:
Thanks for suggestion Jeff, but as I mentioned in my initial post I have
tried that approach. I might have gone wrong about doing though, so any
suggestions as to how to do it the right way would be great. VB.net has
CType function that allows you to do the casting/converting between types,
so
I tried using it by saying
CType(ojbect1, FileSystemWatcher) and then comparing it to the source
object
that already suppose to be of that type. I've also tried casting the
source
object to the same type, but VB throws exceptions not liking one of those
particualar conversions.

Jeff Gaines said:
On 13/10/2004 Alex wrote:

Hi! I need to compare objects that I store in HashTable by using Add
method and passing object as first param, and a unique key string as
second. Later on I iterate through this collection and need to
compare the object stored in the HashTable to another object that's
obtained as one of the params in a call to an event.
Example code (i'm skipping most declarations to avoid long post):

[snip]


I also tried using myEnumerator.Current when performing comparison,
as well as trying to cast the .Value and .Current to the same type of
object that I compare it to (FileSystemWatcher). I'm open to any
suggestions. The basic goal of what I'm doing is to store a
reference to an object and an associated string somewhere so that I
can later when the event of FileSystemObject fires identify that
object and associated string to perform some relevant action. I have
a pretty short Deadline on this so any help would be appreciated!

Thanks!

Can you cast objects in VB.NET?

In C# I would use:

// Get corresponding TreeNode object
TreeNode tn = (TreeNode)hashTable[currentHandle];

Where the HashTable contains pairs of TreeNodes/TreeNode handles.

There must be something similaer in VB, perhaps
hashTable[currentHandle] as TreeNode?
 
J

Jon Skeet [C# MVP]

Alex said:
Stefan, I don't think you fully understand what I'm doing thus far and why I
am having the problem. Hashtable (in case you don't know) is a collection of
sorts where you can store associated pairs of objects(anything, strings,
numbers, objects,etc...) and keys (strings) that identify them.

Um, hashtable keys don't have to be strings. Indeed, if (as per your
first post) you're calling Add and passing an object as the first
parameter and a string as the second parameter, then that object is the
key, and the string is the value - so no wonder the value is never the
FileSystemWatcher you're interested in.
 
G

Guest

Heh! That sure explains a lot :)
Thanks for pointing out my obvious mistake, I'll try the same code I had,
but will take into account the sequence of params now. I hope this will work
(i know it would defenetly not work the way it's written now). I'll let you
know where I get witht this, but thanks again... sometimes you need a fresh
eye/person to find something as simple as this.
 
J

Jeff Gaines

Heh! That sure explains a lot :)
Thanks for pointing out my obvious mistake, I'll try the same code I
had, but will take into account the sequence of params now. I hope
this will work (i know it would defenetly not work the way it's
written now). I'll let you know where I get witht this, but thanks
again... sometimes you need a fresh eye/person to find something as
simple as this.

[snip]

Alex

Don't forget you don't need to iterate through the Hashtable - my C#
example and tefan's VB example shows how to read it directly. Will save
some time :)
 
G

Guest

Hi Jeff! Great news, the bloody thing is working finally. Although there as
some confusion after I set all the sequenses right, the actual comparison of
the objects had to be debugged a couple of times since it's very missleading
the way hashtable works. I assumed that by comparing the .Value or .Current
property of the HT object to the current fired FSW object I'd see the
results, but apparently the actual object is stored in .Key property, which
is the confusing part since I though it would return the Key I stored for
that particular object. But at the end it all came down to comparing the
..Key property to the instance that raised the event, and by getting the
actual key i stored using the .Value property. I'll take a look at the
suggestion about not using the iteration and just finding the correct object
as you said, but I'm not sure that you can access the actual .Value and .Key
property of whatever you find with the method you mentioned. Also, any
suggesions on how the best way either dispose of this filewatchers once they
are processed (don't want them sitting in memory) or re-use them by pooling
them somehow (with threading?). Thanks again!

Cheers

Jeff Gaines said:
Heh! That sure explains a lot :)
Thanks for pointing out my obvious mistake, I'll try the same code I
had, but will take into account the sequence of params now. I hope
this will work (i know it would defenetly not work the way it's
written now). I'll let you know where I get witht this, but thanks
again... sometimes you need a fresh eye/person to find something as
simple as this.

[snip]

Alex

Don't forget you don't need to iterate through the Hashtable - my C#
example and tefan's VB example shows how to read it directly. Will save
some time :)
 
J

Jon Skeet [C# MVP]

Alex said:
Hi Jeff! Great news, the bloody thing is working finally. Although there as
some confusion after I set all the sequenses right, the actual comparison of
the objects had to be debugged a couple of times since it's very missleading
the way hashtable works. I assumed that by comparing the .Value or .Current
property of the HT object to the current fired FSW object I'd see the
results, but apparently the actual object is stored in .Key property, which
is the confusing part since I though it would return the Key I stored for
that particular object.

Hashtable itself doesn't have either Current or Value properties. Are
you talking about IDictionaryEntry or something similar?

If so, I suspect it's a case of getting things the wrong way round
again. I've never seen keys and values getting swapped round as you're
describing.
But at the end it all came down to comparing the .Key property to the
instance that raised the event, and by getting the actual key i
stored using the .Value property. I'll take a look at the suggestion
about not using the iteration and just finding the correct object as
you said, but I'm not sure that you can access the actual .Value and
.Key property of whatever you find with the method you mentioned.

The method Jeff was suggesting is fetching the value using the key -
it's the normal way of accessing a hsahtable. You need to be very clear
on which is the key and which is the value though - this seems to be
the cause of much of the problem.
 
S

Stefan Simek

See inline:

Alex said:
Stefan, I don't think you fully understand what I'm doing thus far and why
I
am having the problem. Hashtable (in case you don't know) is a collection
of
sorts where you can store associated pairs of objects(anything, strings,
numbers, objects,etc...) and keys (strings) that identify them. You can
then
use various methods of HashTable class to walk through the list of items
you
have stored in it and return the objects you stored and the associated
keys,
or use the key to locate a specific object.

Except that using a hashtable just to walk through it is something like
using a DataTable to store integeres from 20 to 1 and retrieve them by index
;)

And no, the keys are not strings. Hashtable is a generic object to object
dictionary, so you just might use the FSW's as keys, and your related
strings as values.

And if my example hasn't been clear enough, I'm sorry, 'cause I don't use
VB.NET, though I usually can read and understand it.
My problem is that I cannot use the key to search for the object I need, I
need to compare the object stored in the HashTable to the object that's
being
passed to the OnChanged event of the FileWatcher that fires at a
particular
time. Once I am able to establish which specific object stored in the
HashTable is the one that's currently reaising it's event, I can get the
associated Key that I stored in HashTable to perform further actions in
the
database. I hope you or anyone else will have some suggestions either to
improve this method or to do it some other way to achieve end result.

And I hope someone will persuade you not to iterate over the hashtable.
Doing so is usefull only if you really need to do something with EVERY
key/value pair, like serializing, etc. The primary function of hashtable is
to lookup a value by it's key, through it's indexer (or Item property, don't
know how it's called in VB).

HTH,
Stefan
Ctefan Cimek said:
What exactly is in the hashtable?

If you want to map a string to the FileSystemWatcher, then you should use
the fsw's as keys and strings as values. And if you are doing so, simply
casting the return value of the hshtable[fsw] call to string should be
enough. So I guess your code should be:

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Dim fsw as FileSystemWatcher
Dim val as String
fsw = source
val = hshtable[fsw]

'if the val isn't Nothing, you've got what you need.

HTH,
Stefan

Alex said:
Thanks for suggestion Jeff, but as I mentioned in my initial post I
have
tried that approach. I might have gone wrong about doing though, so any
suggestions as to how to do it the right way would be great. VB.net
has
CType function that allows you to do the casting/converting between
types,
so
I tried using it by saying
CType(ojbect1, FileSystemWatcher) and then comparing it to the source
object
that already suppose to be of that type. I've also tried casting the
source
object to the same type, but VB throws exceptions not liking one of
those
particualar conversions.

:

On 13/10/2004 Alex wrote:

Hi! I need to compare objects that I store in HashTable by using Add
method and passing object as first param, and a unique key string as
second. Later on I iterate through this collection and need to
compare the object stored in the HashTable to another object that's
obtained as one of the params in a call to an event.
Example code (i'm skipping most declarations to avoid long post):

[snip]


I also tried using myEnumerator.Current when performing comparison,
as well as trying to cast the .Value and .Current to the same type
of
object that I compare it to (FileSystemWatcher). I'm open to any
suggestions. The basic goal of what I'm doing is to store a
reference to an object and an associated string somewhere so that I
can later when the event of FileSystemObject fires identify that
object and associated string to perform some relevant action. I
have
a pretty short Deadline on this so any help would be appreciated!

Thanks!

Can you cast objects in VB.NET?

In C# I would use:

// Get corresponding TreeNode object
TreeNode tn = (TreeNode)hashTable[currentHandle];

Where the HashTable contains pairs of TreeNodes/TreeNode handles.

There must be something similaer in VB, perhaps
hashTable[currentHandle] as TreeNode?
 
G

Guest

Hi Jon! To answer your question, I use the IDictionaryEnumerator to get
those values to walk through the hashtable. I think I got a handle on the
whole key/value issue, and will try to use this method to identify the
necessary object without scrolling through all entries. I do need some
suggestions in regard to properly releasing the FSW objects from memory once
they are fired and actions performed in their OnChanged event. I'm thinking
of first removing their instance from the hashtable, and then calling the
dispose method. I don't think that will remove them from memory though, I
belive the GC will do that later one.

Thanks again!
 
J

Jon Skeet [C# MVP]

Alex said:
Hi Jon! To answer your question, I use the IDictionaryEnumerator to get
those values to walk through the hashtable. I think I got a handle on the
whole key/value issue, and will try to use this method to identify the
necessary object without scrolling through all entries.
Good.

I do need some
suggestions in regard to properly releasing the FSW objects from memory once
they are fired and actions performed in their OnChanged event. I'm thinking
of first removing their instance from the hashtable, and then calling the
dispose method. I don't think that will remove them from memory though, I
belive the GC will do that later one.

That sounds right to me, yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top