473,791 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1509
"ad" <fl****@wfes.tc c.edu.tw> wrote in message news:Or******** ******@tk2msftn gp13.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.Cont ainsKey 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.Conta insKey() Method

Bill
Nov 17 '05 #2
Use the HashTable.Conta insKey() method.

"ad" <fl****@wfes.tc c.edu.tw> wrote in message
news:Or******** ******@tk2msftn gp13.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.co m> ¼¶¼g©ó¶l¥ó·s»D: Anuef.19561$Mr4 .7934@trnddc08. ..
"ad" <fl****@wfes.tc c.edu.tw> wrote in message
news:Or******** ******@tk2msftn gp13.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.Cont ainsKey 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.Conta insKey() 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
6889
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 --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
10
348
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
1691
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 on string). So, for example, when I want to know if the file "xyz.abc" is in the cache, I can write myTable.ContainsKey ( "xyz.abc" ) or if a given DB key is in the hash I can write myTable.ContainsKey ( 123 ). Or do the keys all have to be of...
2
1373
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 c# or VB.NET? I don't want to use JScript. consFin = {2:'m',4:'n',6:'ng',8:'r',10:'l',12:'kh',14:'k',16:'s',18:'hl',19:'tl',20:'s
18
3146
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
2657
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 that I cannot alter the value. The documentation for using an enumerator and moveNext also have the same restriction. WONT WORK: private Hashtable hashValueNuller(Hashtable theHash) {
8
5870
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 = str1.GetHashCode(); // Returns 175803953 Hashtable ht = new Hashtable(); ht.Add(hash ,strValue); ht.Add(hash1,strValue1); // ****ERROR****
2
1345
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: ****BEGIN CODE****
1
1186
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: ****BEGIN CODE****
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.