473,396 Members | 2,016 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,396 software developers and data experts.

Print out contents of a hashtable

I can't believe I've stumbled on a simple problem such as this. After
all these years, that too.

Anyway, it goes that I just realized that I could not iterate through
my hashtable and print out its contents. On further probing, I realized
that the hashtable didn't implenent IList, which really has the
this[int] indexer. And so I couldnt do this:

for (int i = 0; i < _myHashTable.Count; i++)
Console.WriteLine(_myHashTable[i].ToString());

I had to know the key to index the hashtable elements. I could
enumerate through it by getting its enumerator
(IEnumerable.GetEnumerator) and then calling Next() and Current on its
IEnumerator but even then, I'd have to know the keys.

I took the hashtable in the first place because of its Contains method.
It can easily reference its elements and check for the existence of a
key, in constant time whereas list takes linear time.

Now, I am stuck. All I need to do is print out the contents of the
hashtable. The other option I can consider is to take a Dictionary or a
NameValueCollection or to take an ArrayList and extend it to have a
Contains look up method.

Sep 7 '06 #1
4 10321
People, never mind, I am using a System.Collections.SortedList. It's
got what I need.

bool Contains(key);
object GetKey(int);

And I don't mind if the items are all sorted.

Sep 7 '06 #2
Water Cooler v2,

Think of the hashtable as a collection of DictionaryEntry objects, with your
custom object stored in the DictionaryEntry's Value property.

Use a For Each loop to iterate through the hashtable and cast your object
from DictionaryEntry.Value:

For Each de As DictionaryEntry in myHashTable
myObject = DirectCast (de.Value, myClass)
Next

Kerry Moorman
"Water Cooler v2" wrote:
I can't believe I've stumbled on a simple problem such as this. After
all these years, that too.

Anyway, it goes that I just realized that I could not iterate through
my hashtable and print out its contents. On further probing, I realized
that the hashtable didn't implenent IList, which really has the
this[int] indexer. And so I couldnt do this:

for (int i = 0; i < _myHashTable.Count; i++)
Console.WriteLine(_myHashTable[i].ToString());

I had to know the key to index the hashtable elements. I could
enumerate through it by getting its enumerator
(IEnumerable.GetEnumerator) and then calling Next() and Current on its
IEnumerator but even then, I'd have to know the keys.

I took the hashtable in the first place because of its Contains method.
It can easily reference its elements and check for the existence of a
key, in constant time whereas list takes linear time.

Now, I am stuck. All I need to do is print out the contents of the
hashtable. The other option I can consider is to take a Dictionary or a
NameValueCollection or to take an ArrayList and extend it to have a
Contains look up method.

Sep 7 '06 #3
To iterate:
foreach (DictionaryEntry entry in t)
{
Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
}

If you want to check if the hash table contains a key/value, try using
ContainKey (same as Contains, but method name makes it clearer) and
ContainsValue methods.

Water Cooler v2 wrote:
I can't believe I've stumbled on a simple problem such as this. After
all these years, that too.

Anyway, it goes that I just realized that I could not iterate through
my hashtable and print out its contents. On further probing, I realized
that the hashtable didn't implenent IList, which really has the
this[int] indexer. And so I couldnt do this:

for (int i = 0; i < _myHashTable.Count; i++)
Console.WriteLine(_myHashTable[i].ToString());

I had to know the key to index the hashtable elements. I could
enumerate through it by getting its enumerator
(IEnumerable.GetEnumerator) and then calling Next() and Current on its
IEnumerator but even then, I'd have to know the keys.

I took the hashtable in the first place because of its Contains method.
It can easily reference its elements and check for the existence of a
key, in constant time whereas list takes linear time.

Now, I am stuck. All I need to do is print out the contents of the
hashtable. The other option I can consider is to take a Dictionary or a
NameValueCollection or to take an ArrayList and extend it to have a
Contains look up method.
Sep 7 '06 #4
Oh yeah! Thanks, Kerry and Truong. I was wondering how I could cast the
"object" inside the hashtable into an object that looked like a
dictionaryentry. Only, I didn't see the obvious.

Sep 7 '06 #5

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

Similar topics

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...
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
16
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
1
by: yer darn tootin | last post by:
Ahoy, I have an aspx web page that loads an ascx control that's populated with various items ( in a datalist ). Select an item and the page reloads and the datalist now shows the details. ...
12
by: Peter Lin | last post by:
Hey, I am just wondering if anyone has got any idea of setting up a new class so that you could just print like the old ways with the printer class, since I am writing a program that really...
4
by: Water Cooler v2 | last post by:
I can't believe I've stumbled on a simple problem such as this. After all these years, that too. Anyway, it goes that I just realized that I could not iterate through my hashtable and print out...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
2
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that...
4
by: solanki | last post by:
> Hi Folks, > > I am finding a small trouble while printing the entire contents of an > Iframe which carries an html report which is in turn generated by > jasper reports.s > > Hear is the...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.