471,312 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

Collection Advice Requested

Using .NET 3.5... I have the following class:

class NodeDescriptor
{
// Properties only in this class
public int NodeID { get; private set; }
public int ParentNodeID { get; private set; }
public string SomeStringValue { get; set; }
// etc... (2 more int and 7 more string properties, that's all.)
}
I need to create a collection of these NodeDescriptor objects. My question
is about how to best implement this collection. This collection needs to
expose a method (named something like GetChildNodes()) that returns all of
the immediate child nodes of a given node. So, for example, if the
collection contains 5 NodeDescriptor instances with the following property
values...

NodeID -- ParentNodeID -- SomeStringValue
23 -- null -- "some string"
37 -- 23 -- "another string"
19 -- 23 -- "yet another"
94 -- 37 -- "okey dokey"
51 -- 23 -- "yes, of course"

.... the GetChildNodes() method would return the NodeDescriptor instances
with NodeID = 37, 19, and 51 when passed NodeID = 23 (because the
parentNodeID = 23 for each of these NodeDescriptor objects).

How can I best implement this collection - given the objectives of NOT
looping through each contained NodeDescriptor instance? List<NodeDescriptor>
would not work for me because I'd have to loop through it, as far as I know,
and I don't want to loop. Can LINQ be used here - or would that be overkill?
If possible I'd like the GetChildNodes() method to return a collection of
NodeDescriptor objects.

A couple of relevant considerations:
1. Only imediate child nodes, and *not* grand child nodes, are to be
returned. So the NodeDescriptor instance with NodeID = 94 would not be
returned when the GetChildNodes() method is passed NodeID = 23 (because it's
ParentNodeID is 37).

2. This collection will typically contain 5-8 (rarely more than 12, and
never more than 30) NodeDescriptor instances.

3. The NodeDescriptor instances should be able to be inserted into this
collection collection in any arbitrary sequence (i.e., no particular
sequence is assumed).

Thanks!

Jun 27 '08 #1
2 1038
On May 21, 4:30 pm, "Jordan S." <A...@B.comwrote:
Using .NET 3.5... I have the following class:

class NodeDescriptor
{
// Properties only in this class
public int NodeID { get; private set; }
public int ParentNodeID { get; private set; }
public string SomeStringValue { get; set; }
// etc... (2 more int and 7 more string properties, that's all.)

}

I need to create a collection of these NodeDescriptor objects. My question
is about how to best implement this collection. This collection needs to
expose a method (named something like GetChildNodes()) that returns all of
the immediate child nodes of a given node. So, for example, if the
collection contains 5 NodeDescriptor instances with the following property
values...

NodeID -- ParentNodeID -- SomeStringValue
23 -- null -- "some string"
37 -- 23 -- "another string"
19 -- 23 -- "yet another"
94 -- 37 -- "okey dokey"
51 -- 23 -- "yes, of course"

... the GetChildNodes() method would return the NodeDescriptor instances
with NodeID = 37, 19, and 51 when passed NodeID = 23 (because the
parentNodeID = 23 for each of these NodeDescriptor objects).

How can I best implement this collection - given the objectives of NOT
looping through each contained NodeDescriptor instance? List<NodeDescriptor>
would not work for me because I'd have to loop through it, as far as I know,
and I don't want to loop. Can LINQ be used here - or would that be overkill?
If possible I'd like the GetChildNodes() method to return a collection of
NodeDescriptor objects.

A couple of relevant considerations:
1. Only imediate child nodes, and *not* grand child nodes, are to be
returned. So the NodeDescriptor instance with NodeID = 94 would not be
returned when the GetChildNodes() method is passed NodeID = 23 (because it's
ParentNodeID is 37).

2. This collection will typically contain 5-8 (rarely more than 12, and
never more than 30) NodeDescriptor instances.

3. The NodeDescriptor instances should be able to be inserted into this
collection collection in any arbitrary sequence (i.e., no particular
sequence is assumed).

Thanks!
Heres one implementation.. not saying its the best.

Its the same thing you have but with few changes.

public int NodeID { get; private set; }
public NodeDescriptor ParentNode { get; private set; }
private List<NodeDescriptorchildren ;
everytime you add a child node you will have to add a referce to the
current node as parent.

I think that should do what you are trying to do
Jun 27 '08 #2
On May 21, 9:30 pm, "Jordan S." <A...@B.comwrote:

<snip>
I need to create a collection of these NodeDescriptor objects. My question
is about how to best implement this collection. This collection needs to
expose a method (named something like GetChildNodes()) that returns all of
the immediate child nodes of a given node.
Sounds like you want an IDictionary<int,IEnumerable<NodeDescriptor>-
or in more concrete terms, something like

Dictionary<int,List<NodeDescriptor>>

See http://msmvps.com/blogs/jon.skeet/ar...tiful-one.aspx
for some code which may help.

Jon
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris Dunaway | last post: by
5 posts views Thread by Miljana | last post: by
reply views Thread by rosydwin | last post: by

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.