By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

hashtable collection in a class--help!

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.