473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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?
Nov 17 '05 #1
5 1487
"ad" <fl****@wfes.tcc.edu.tw> wrote in message news:Or**************@tk2msftngp13.phx.gbl...
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
Nov 17 '05 #2
Use the HashTable.ContainsKey() method.

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:Or**************@tk2msftngp13.phx.gbl...
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?

Nov 17 '05 #3
ad
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" <qw****@asdf.com> ¼¶¼g©ó¶l¥ó·s»D:Anuef.19561$Mr4.7934@trnddc08...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:Or**************@tk2msftngp13.phx.gbl...
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

Nov 17 '05 #4
>But is sKey is not in the myHash.Keys, it raise an Exception.
If I remember correctly, it returns null in that case.

Nov 17 '05 #5
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

ad wrote:
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?

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
10
by: SK | last post by:
Hey, i have a hashtable, where I am adding some values. Now when I iterate through them then they start in reverse order, why is that happening and how can I get rid of it? Thanks
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
2
by: mark4asp | last post by:
Hello, I am converting an app from JavaScript to ASP.NET I have the following JavaScript 'associative array'. Is there a concise way to create an analogous structure in .NET Framework using...
18
by: JezB | last post by:
How can I loop thru a hashtable changing the Value of each entry ? Whatever I try I always seem to get the error about modifying the collection within the loop.
2
by: andrewcw | last post by:
I have a reason to resuse hashtables and I'd like to set the values to null without having to reload the keys.- > which is what the clear method does. If I try a foreach construct I get an error...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
2
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
1
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.