473,385 Members | 1,553 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,385 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 3891
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.