How to dertiminate if a sting in the keys of a hashtable

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have a hashtable name myHash.
sKey is a string, I use myHash[sKey] to retrieve the value of that key.

But is sKey is not in the myHash.Keys, it raise an Exception.

I want to dertiminate if sKey in myHash.Keys.

How can I do that?
 
ad said:
I have a hashtable name myHash.
sKey is a string, I use myHash[sKey] to retrieve the value of that key.

But is sKey is not in the myHash.Keys, it raise an Exception.

I want to dertiminate if sKey in myHash.Keys.

How can I do that?

I looked at your post.
Started up the SDK documentation (a few seconds)
Typed in "Hashtable." (another few seconds)
And saw "Hashtable.ContainsKey Method" Staring me in the face.

Now granted, I already knew the answer to the question, but it is the OBVIOUS place to look. And,
the method name sort of jumps right out at you.....So the question is....

Why post the question here (few minutes of typing) when it is REALLY easy to find in the
documentation (Few seconds of typing)???

Anyway....Look at the Hashtable.ContainsKey() Method

Bill
 
Use the HashTable.ContainsKey() method.

I have a hashtable name myHash.
sKey is a string, I use myHash[sKey] to retrieve the value of that key.

But is sKey is not in the myHash.Keys, it raise an Exception.

I want to dertiminate if sKey in myHash.Keys.

How can I do that?
 
Thanks,
I have studied the documents before, but my direction is wrong, I drill down
the ICollection, it is the type of HashTable.Keys.


Bill Butler said:
ad said:
I have a hashtable name myHash.
sKey is a string, I use myHash[sKey] to retrieve the value of that key.

But is sKey is not in the myHash.Keys, it raise an Exception.

I want to dertiminate if sKey in myHash.Keys.

How can I do that?

I looked at your post.
Started up the SDK documentation (a few seconds)
Typed in "Hashtable." (another few seconds)
And saw "Hashtable.ContainsKey Method" Staring me in the face.

Now granted, I already knew the answer to the question, but it is the
OBVIOUS place to look. And, the method name sort of jumps right out at
you.....So the question is....

Why post the question here (few minutes of typing) when it is REALLY easy
to find in the documentation (Few seconds of typing)???

Anyway....Look at the Hashtable.ContainsKey() Method

Bill
 
But is sKey is not in the myHash.Keys, it raise an Exception.
If I remember correctly, it returns null in that case.
 
You are probably casting the return causing the exception. This is a
good spot to use the as keyword instead of casting.
Foo foo = myHash[sKey] as Foo;
if (foo == null) // foo will be null if not there
return;

Hope this helps
Leon Lambert
 
Back
Top