473,385 Members | 2,003 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.

hashtable collection in a class--help!

I'm reasonably new to C#, and using a class containing a hashtable
collection to feed a treeview. My problem is that I can't pull the
data from the hashtable!

Here's the code I have:

if (myItem.DataFields.Count != 0)
foreach (string name in myItem.DataFields.Values) {
treeView1.SelectedNode.Nodes.Add (name); // Error on this line
}

myItem is an instance of my class, DataFields is the hashtable, and
you can see I'm trying to access the column containing values. I get
a 'System.NullReferenceException' at runtime.

Can someone help? Am I approaching this whole thing wrong?

Thanks,
Brian
Nov 16 '05 #1
10 2795
Brian,

A Hashtable is for accessing members via a key. I've never used a Hashtable
in this way before; if you simply want to store a list of objects to
retreive, an ArrayList is much more lightweight.

Having said that -- assuming those really are strings you've stored into the
Hashtable prior to this -- along the lines of
myItem.DataFields.Add(somekey,"Foo") -- then you should be able to retreive
them like so:

foreach (DictionaryEntry de in myItem.DataFields) {
treeView1.SelectedNode.Nodes.Add((string)de.Value) ;
}

You might see if that works ...

--Bob

"Brian" <br*********@ky.gov> wrote in message
news:63**************************@posting.google.c om...
I'm reasonably new to C#, and using a class containing a hashtable
collection to feed a treeview. My problem is that I can't pull the
data from the hashtable!

Here's the code I have:

if (myItem.DataFields.Count != 0)
foreach (string name in myItem.DataFields.Values) {
treeView1.SelectedNode.Nodes.Add (name); // Error on this line
}

myItem is an instance of my class, DataFields is the hashtable, and
you can see I'm trying to access the column containing values. I get
a 'System.NullReferenceException' at runtime.

Can someone help? Am I approaching this whole thing wrong?

Thanks,
Brian

Nov 16 '05 #2
Brian <br*********@ky.gov> wrote:
I'm reasonably new to C#, and using a class containing a hashtable
collection to feed a treeview. My problem is that I can't pull the
data from the hashtable!

Here's the code I have:

if (myItem.DataFields.Count != 0)
foreach (string name in myItem.DataFields.Values) {
treeView1.SelectedNode.Nodes.Add (name); // Error on this line
}

myItem is an instance of my class, DataFields is the hashtable, and
you can see I'm trying to access the column containing values. I get
a 'System.NullReferenceException' at runtime.

Can someone help? Am I approaching this whole thing wrong?


The fact that you've got a NullReference exception suggests that you've
probably not got a SelectedNode.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Thanks for the response, Bob. I actually am using the hash table to
store key/value pairs. I was trying to add just the values to my
treeview.

The code you gave me below creates an error message:

SystemNullReferenceException
Additional Info: Object reference not set to an instance of an object

Any ideas what is going on?

--------------
Brian,

A Hashtable is for accessing members via a key. I've never used a
Hashtable
in this way before; if you simply want to store a list of objects to
retreive, an ArrayList is much more lightweight.

Having said that -- assuming those really are strings you've stored into
the
Hashtable prior to this -- along the lines of
myItem.DataFields.Add(somekey,"Foo") -- then you should be able to
retreive
them like so:

foreach (DictionaryEntry de in myItem.DataFields) {
treeView1.SelectedNode.Nodes.Add((string)de.Value) ;
}

You might see if that works ...

--Bob

"Brian" <br*********@ky.gov> wrote in message
news:63**************************@posting.google.c om...
I'm reasonably new to C#, and using a class containing a hashtable
collection to feed a treeview. My problem is that I can't pull the
data from the hashtable!

Here's the code I have:

if (myItem.DataFields.Count != 0)
foreach (string name in myItem.DataFields.Values) {
treeView1.SelectedNode.Nodes.Add (name); // Error on this line
}

myItem is an instance of my class, DataFields is the hashtable, and
you can see I'm trying to access the column containing values. I get
a 'System.NullReferenceException' at runtime.

Can someone help? Am I approaching this whole thing wrong?

Thanks,
Brian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
I just had a breakthrough! :)

All I need to do now is make the node I just added the currently
selected node. Here's my code:

foreach (DataItem myItem in CategoryArrayList) {

treeView1.Nodes.Add (new TreeNode (myItem.Name));

if (myItem.DataFields.Count != 0)
foreach (string name in
myItem.DataFields)treeView1.SelectedNode.Nodes.Add (name);
}

I've tried TreeNode tn = treeView1.Nodes.Add (new TreeNode
(myItem.Name));

But that produces an error, because apparently the Add method returns an
int. How can I do this with my current code?

Thanks,
Brian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Create the node using a string "my node". That overload returns the new
treeNode that was created.
Did below from memory, but should work or be close.

TreeNode tn = null;
foreach(DataItem myItem in CategoryArrayList)
{
tn = treeView1.Nodes.Add(myItem.Name);
if (myItem.DataFields.Count > 0 )
{
foreach(string name in myItem.DataFields)
{
tn.Nodes.Add(name); // don't need the returned node, so we
don't store the ref.
}
}
}
--
William Stacey, MVP

"Brian Kiser" <br*********@ky.gov> wrote in message
news:#3**************@TK2MSFTNGP11.phx.gbl...
I just had a breakthrough! :)

All I need to do now is make the node I just added the currently
selected node. Here's my code:

foreach (DataItem myItem in CategoryArrayList) {

treeView1.Nodes.Add (new TreeNode (myItem.Name));

if (myItem.DataFields.Count != 0)
foreach (string name in
myItem.DataFields)treeView1.SelectedNode.Nodes.Add (name);
}

I've tried TreeNode tn = treeView1.Nodes.Add (new TreeNode
(myItem.Name));

But that produces an error, because apparently the Add method returns an
int. How can I do this with my current code?

Thanks,
Brian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #6
Actually you don't need the if test either.

--
William Stacey, MVP

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
Create the node using a string "my node". That overload returns the new
treeNode that was created.
Did below from memory, but should work or be close.

TreeNode tn = null;
foreach(DataItem myItem in CategoryArrayList)
{
tn = treeView1.Nodes.Add(myItem.Name);
if (myItem.DataFields.Count > 0 )
{
foreach(string name in myItem.DataFields)
{
tn.Nodes.Add(name); // don't need the returned node, so we
don't store the ref.
}
}
}
--
William Stacey, MVP

"Brian Kiser" <br*********@ky.gov> wrote in message
news:#3**************@TK2MSFTNGP11.phx.gbl...
I just had a breakthrough! :)

All I need to do now is make the node I just added the currently
selected node. Here's my code:

foreach (DataItem myItem in CategoryArrayList) {

treeView1.Nodes.Add (new TreeNode (myItem.Name));

if (myItem.DataFields.Count != 0)
foreach (string name in
myItem.DataFields)treeView1.SelectedNode.Nodes.Add (name);
}

I've tried TreeNode tn = treeView1.Nodes.Add (new TreeNode
(myItem.Name));

But that produces an error, because apparently the Add method returns an
int. How can I do this with my current code?

Thanks,
Brian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #7
William,

That did it! You are a real life saver. I appreciate the help.

Although I really love C#, I sometimes wonder if I'm ever going to learn
it.

Thanks again.

-Brian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #8
Just explore like your doing is best way to learn. Intellisense is a big
help as you can cycle around the overloads to quickly see the return types
on the overloads which helps (me) figure out the object model quickly. I
just wrote a raw treeview, treenode classes as I needed n-ary tree without
all the gui stuff and needed some other behaviors that treeview did not give
me and was quicker to write a custom class. When I need to display my tree
in a treeview, I just fill the GUI TreeView with my treeview using about
five lines in a recursive function. Cheers!

--
William Stacey, MVP

"Brian Kiser" <br*********@ky.gov> wrote in message
news:#h*************@tk2msftngp13.phx.gbl...
William,

That did it! You are a real life saver. I appreciate the help.

Although I really love C#, I sometimes wonder if I'm ever going to learn
it.

Thanks again.

-Brian

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #9


----- Brian Kiser wrote: ----
Although I really love C#, I sometimes wonder if I'm ever going to learn it


To master the .NET framework, you HAVE TO know how to read documentation. all the information that could have helped you are very well documented. with thousands of classes in the existing framework, and thousands more to come in the near future, people are not gonna get very far if they have to be spoon-fed every simple method call.
Nov 16 '05 #10
I don't think anyone "spoon fed" me. I've read page after page after
page of documentation today, just to get where I am now. I could not
find the particular information I needed.

Thankfully, there are newsgroups and helpful people out there when you
get stuck.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #11

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

Similar topics

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.
4
by: Vladimir C. | last post by:
Hashtable map = new Hashtable(); map = 10; map = 20; foreach(DictionaryEntry e in map) { e.Value = 100; Console.WriteLine("{0}: {1}", key, map); }
7
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key?...
8
by: JackRazz | last post by:
Is it possible to create a hashtable that doesn't store the key? I have a very large hashtable and I just want it to store the HashCode and the Value (two Int16s). Thanks - JackRazz
2
by: Bernard Bourée | last post by:
I have a Class named Variable with 2 properties "Name" and "Value" I have grouped them in a Hashtable collection Dim ColVar as Hashtable when I try to go through the collection with Dim var...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
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....
4
by: Adam Right | last post by:
How can i add a collection to another collection ? For example : --------------------------------------------- StringCollection strColl= new StringCollection(); //string series Hashtable...
2
by: Mark S. | last post by:
Hello, I have a static class with several hundred private static List<>. Does the ..NET framework keep an internal List/Hashtable/Collection of all my Lists that I can loop over? If so, please...
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: 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?
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
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...
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,...

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.