473,320 Members | 1,848 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,320 software developers and data experts.

Hashtable fails miserably.. i not sure why this cannot work???

Hi i very wonder, why i can't get the right output.

I search for -100, and it extracts -99 for me. I place any number, but i stil get -99. What is the problem???

Hashtable table = new Hashtable();

table.Add(-100, "value1");
table.Add(-99, "value2");
table.Add(-98, "value3");
table.Add(-97, "value4");
table.Add(-96, "value5");
table.Add(-95, "value6");
table.Add(-94, "value7");
table.Add(-93, "value8");
table.Add(-92, "value9");
table.Add(-91, "value10");
table.Add(-90, "value11");
table.Add(-89, "value12");
table.Add(-88, "value13");
table.Add(-87, "value14");
table.Add(-86, "value15");
//search by key
int keyToFind = -100;

foreach(DictionaryEntry d in table)
{
if (table.Contains(keyToFind))
{
Console.WriteLine ("{0}\t{1}", d.Key, d.Value);
break;
}
}

Any idea, please? Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
1 1006
That is not the way to lookup a value in a Hashtable. Do it like this:

//search by key
int keyToFind = -100;
Console.WriteLine(table[keyToFind]);

foreach just loops through each key/value pair and the order is NOT
guaranteed no matter what order you add them in.. Contains just indicates
whether a particular value exists, it does nothing to look it up and doesn't
change the current value of the foreach loop.

Richard

"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:CD**********************************@microsof t.com...
Hi i very wonder, why i can't get the right output.

I search for -100, and it extracts -99 for me. I place any number, but i stil get -99. What is the problem???
Hashtable table = new Hashtable();

table.Add(-100, "value1");
table.Add(-99, "value2");
table.Add(-98, "value3");
table.Add(-97, "value4");
table.Add(-96, "value5");
table.Add(-95, "value6");
table.Add(-94, "value7");
table.Add(-93, "value8");
table.Add(-92, "value9");
table.Add(-91, "value10");
table.Add(-90, "value11");
table.Add(-89, "value12");
table.Add(-88, "value13");
table.Add(-87, "value14");
table.Add(-86, "value15");
//search by key
int keyToFind = -100;

foreach(DictionaryEntry d in table)
{
if (table.Contains(keyToFind))
{
Console.WriteLine ("{0}\t{1}", d.Key, d.Value);
break;
}
}

Any idea, please? Thanks.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2

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

Similar topics

1
by: s88 | last post by:
I work with the bit-vector(http://search.cpan.org/dist/Bit-Vector/) and squid lib as well. But I meet a weird situation now...Let's see my code. I wanna creat a hashtable in a function, and then...
4
by: André Giesing | last post by:
Hello NG! I've got a short question: Is there a difference of the internal representation of a Hashtable between the Compact Framework and the normal Framework? I've got the suspicion,...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
9
by: Oberon | last post by:
My HashTable (Global.Games) is a static collection of objects of type Game. A Game object has 8 fields (exposed as properties). The key to the HashTable is also one of these fields (GameID, of type...
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: Scott M. Lyon | last post by:
I'm working on using the COM Interop wrapper on an existing .NET library, to allow existing VB6 applications to call the .NET code. And I've been able to get it working in a basic sense (I can...
5
by: =?Utf-8?B?VG9t?= | last post by:
Cannot not seem to make any sense of the order that my key/values end up in when added to the Hashtable...ideally, I would like to be able to sort the keys/values...but not thinking it is possible....
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.