hashtable collection in a class--help!

B

Brian

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
 
B

Bob Grommes

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
 
J

Jon Skeet [C# MVP]

Brian said:
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.
 
B

Brian Kiser

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
 
B

Brian Kiser

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
 
W

William Stacey [MVP]

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.
}
}
}
 
B

Brian Kiser

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
 
W

William Stacey [MVP]

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!
 
G

Guest

----- 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.
 
B

Brian Kiser

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top