类中的哈希表集合 - 帮助! [英] hashtable collection in a class--help!

查看:68
本文介绍了类中的哈希表集合 - 帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,并且使用包含哈希表

集合的类来提供树视图。我的问题是我无法从哈希表中提取

数据!


这是我的代码:


if(myItem.DataFields.Count!= 0)

foreach(myItem.DataFields.Values中的字符串名称){

treeView1.SelectedNode。 Nodes.Add(name); //这行错误

}


myItem是我班级的一个实例,DataFields是哈希表,

你我可以看到我正在尝试访问包含值的列。我在运行时得到了''
'''System.NullReferenceException''。


有人可以帮忙吗?我接近这整件事了吗?


谢谢,

Brian

解决方案

< blockquote> Brian,


Hashtable用于通过密钥访问成员。我之前从未使用过Hashtable

;如果你只是想将一个对象列表存储到

retreive,那么ArrayList要轻得多。


话虽如此 - 假设那些字符串真的是字符串在此之前你已经存入了

Hashtable - 沿着

myItem.DataFields.Add(somekey,Foo)的行 - 然后你应该能够像他们一样转发




foreach(myEtem.DataFields中的DictionaryEntry de){

treeView1.SelectedNode .Nodes.Add((string)de.Value);

}


你可能会看到它是否有效......

--BOB


" Brian" <峰; br ********* @ ky.gov>在消息中写道

news:63 ************************** @ posting.google.c om ...

我是C#的新手,并且使用包含哈希表
集合的类来提供树视图。我的问题是我无法从哈希表中提取
数据!

这是我的代码:

if(myItem.DataFields) .Count!= 0)
foreach(myItem.DataFields.Values中的字符串名称){
treeView1.SelectedNode.Nodes.Add(name); //这行错误
}

myItem是我的类的一个实例,DataFields是哈希表,你可以看到我正在尝试访问包含的列值。我在运行时得到''System.NullReferenceException''。

有人可以帮忙吗?我接近这一切都错了吗?

谢谢,
Brian



Brian< br **** *****@ky.gov>写道:

我是C#的新手,并且使用包含哈希表
集合的类来提供树视图。我的问题是我无法从哈希表中提取
数据!

这是我的代码:

if(myItem.DataFields) .Count!= 0)
foreach(myItem.DataFields.Values中的字符串名称){
treeView1.SelectedNode.Nodes.Add(name); //这行错误
}

myItem是我的类的一个实例,DataFields是哈希表,你可以看到我正在尝试访问包含的列值。我在运行时得到''System.NullReferenceException''。

有人可以帮忙吗?我接近这整件事是错误的吗?




事实上你有一个NullReference异常表明你已经可能了b $ b b没有选择的节点。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复对于小组,请不要给我发邮件


感谢您的回复,鲍勃。我实际上是使用哈希表来

存储键/值对。我试图将值添加到我的

树视图中。


您在下面给我的代码会产生一条错误消息:

SystemNullReferenceException

附加信息:对象引用未设置为对象的实例


任何想法是怎么回事?


--------------

Brian,


Hashtable用于访问会员通过钥匙。我以前从未用这种方式使用过

Hashtable

;如果你只是想将一个对象列表存储到

retreive,那么ArrayList要轻得多。


话虽如此 - 假设那些字符串真的是字符串在此之前你已经存储了



Hashtable -

myItem.DataFields.Add(somekey,") ; Foo) - 然后你应该能够

retreive

他们是这样的:


foreach(DictionaryEntry de in myItem.DataFields){

treeView1.SelectedNode.Nodes.Add((string)de.Value);

}


你可能会看到它是否有效...


--BOB


" Brian" <峰; br ********* @ ky.gov>在消息中写道

news:63 ************************** @ posting.google.c om ...

我是C#的新手,并且使用包含哈希表
集合的类来提供树视图。我的问题是我无法从哈希表中提取
数据!

这是我的代码:

if(myItem.DataFields) .Count!= 0)
foreach(myItem.DataFields.Values中的字符串名称){
treeView1.SelectedNode.Nodes.Add(name); //这行错误
}

myItem是我的类的一个实例,DataFields是哈希表,你可以看到我正在尝试访问包含的列值。我在运行时得到''System.NullReferenceException''。

有人可以帮忙吗?我接近这整件事了吗?

谢谢,
Brian



***通过Developersdex发送 http://www.developersdex.com ***

不要只参加USENET。 ..得到奖励!


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

解决方案

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



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


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!


这篇关于类中的哈希表集合 - 帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆