Collection Advice Requested

J

Jordan S.

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

parez

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<NodeDescriptor> children ;
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
 
J

Jon Skeet [C# MVP]

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/a...ple-extension-method-but-a-beautiful-one.aspx
for some code which may help.

Jon
 

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