473,569 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DataFie lds.Count != 0)
foreach (string name in myItem.DataFiel ds.Values) {
treeView1.Selec tedNode.Nodes.A dd (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.NullRef erenceException ' at runtime.

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

Thanks,
Brian
Nov 16 '05 #1
10 2814
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.DataFiel ds.Add(somekey, "Foo") -- then you should be able to retreive
them like so:

foreach (DictionaryEntr y de in myItem.DataFiel ds) {
treeView1.Selec tedNode.Nodes.A dd((string)de.V alue);
}

You might see if that works ...

--Bob

"Brian" <br*********@ky .gov> wrote in message
news:63******** *************** ***@posting.goo gle.com...
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.DataFie lds.Count != 0)
foreach (string name in myItem.DataFiel ds.Values) {
treeView1.Selec tedNode.Nodes.A dd (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.NullRef erenceException ' 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.DataFie lds.Count != 0)
foreach (string name in myItem.DataFiel ds.Values) {
treeView1.Selec tedNode.Nodes.A dd (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.NullRef erenceException ' 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.co m>
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:

SystemNullRefer enceException
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.DataFiel ds.Add(somekey, "Foo") -- then you should be able to
retreive
them like so:

foreach (DictionaryEntr y de in myItem.DataFiel ds) {
treeView1.Selec tedNode.Nodes.A dd((string)de.V alue);
}

You might see if that works ...

--Bob

"Brian" <br*********@ky .gov> wrote in message
news:63******** *************** ***@posting.goo gle.com...
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.DataFie lds.Count != 0)
foreach (string name in myItem.DataFiel ds.Values) {
treeView1.Selec tedNode.Nodes.A dd (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.NullRef erenceException ' 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 CategoryArrayLi st) {

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

if (myItem.DataFie lds.Count != 0)
foreach (string name in
myItem.DataFiel ds)treeView1.Se lectedNode.Node s.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(DataIte m myItem in CategoryArrayLi st)
{
tn = treeView1.Nodes .Add(myItem.Nam e);
if (myItem.DataFie lds.Count > 0 )
{
foreach(string name in myItem.DataFiel ds)
{
tn.Nodes.Add(na me); // 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******** ******@TK2MSFTN GP11.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 CategoryArrayLi st) {

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

if (myItem.DataFie lds.Count != 0)
foreach (string name in
myItem.DataFiel ds)treeView1.Se lectedNode.Node s.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******** ******@tk2msftn gp13.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(DataIte m myItem in CategoryArrayLi st)
{
tn = treeView1.Nodes .Add(myItem.Nam e);
if (myItem.DataFie lds.Count > 0 )
{
foreach(string name in myItem.DataFiel ds)
{
tn.Nodes.Add(na me); // 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******** ******@TK2MSFTN GP11.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 CategoryArrayLi st) {

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

if (myItem.DataFie lds.Count != 0)
foreach (string name in
myItem.DataFiel ds)treeView1.Se lectedNode.Node s.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******** *****@tk2msftng p13.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

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

Similar topics

18
522
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
17469
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
14713
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? I am not quite understand this. And I think NameValueCollection's Set is a nice method. If I use HashTable, how can I update a item in it? Do I...
8
1585
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
1361
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 as Variable For Each var in ColVar
6
13386
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 very little luck processing the Hashtable itself in VB6 (I can add a reference to the project so it knows what a Hashtable is, but I'm not having much...
5
2665
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. For those who are sure going to ask why I am asking this....I use the Hashtable for conveniently cross-referencing pairs of information...and I...
4
4799
by: Adam Right | last post by:
How can i add a collection to another collection ? For example : --------------------------------------------- StringCollection strColl= new StringCollection(); //string series Hashtable hshCol= new Hashtable(); // main collection for string series strColl.Add("white"); //first series starting strColl.Add("hot"); strColl.Add("little");
2
1105
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 show the syntax or docs reference, if it's there I am unable to find it. TIA, M
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.