I have a class that I wrote in C# which has several properties, including a list of the same class that I created. What I'm trying to do is pull out data from a cache and store it as a list of my class. The problem arises in that there is a relationship
between the root nodes and child nodes (lets call it "id"). I'm basically trying to mimic a relationship between two datatables using a key, but I can't figure out how to do that with classes. Can anyone help me out? Thanks in advance.
You're going to need to setup another field, maybe called ParentID, which would hold the ID value for the container. You'd use the same concept in a database table to setup a recursive relationship.
What you're describing is a simplification of the Composite pattern. You'd better take a look at the link on my last post. It will be very useful to you.
Gabriel Bogéa (http://www.gbogea.com)
-----------------
Please 'Mark as Answer' the post(s) that helped you
dan789
Member
20 Points
13 Posts
Recursive Classes
May 09, 2008 07:57 PM|LINK
I have a class that I wrote in C# which has several properties, including a list of the same class that I created. What I'm trying to do is pull out data from a cache and store it as a list of my class. The problem arises in that there is a relationship between the root nodes and child nodes (lets call it "id"). I'm basically trying to mimic a relationship between two datatables using a key, but I can't figure out how to do that with classes. Can anyone help me out? Thanks in advance.
Classes
DotNetAdviso...
Contributor
4576 Points
869 Posts
Re: Recursive Classes
May 09, 2008 08:15 PM|LINK
You're going to need to setup another field, maybe called ParentID, which would hold the ID value for the container. You'd use the same concept in a database table to setup a recursive relationship.
www.dotnetadvisor.com
gbogea
Contributor
4019 Points
576 Posts
Re: Recursive Classes
May 09, 2008 08:23 PM|LINK
Your're trying to build a tree like structure? There's a design pattern to do this called composite. Here's an excellent article about it:
http://www.dofactory.com/Patterns/PatternComposite.aspx
-----------------
Please 'Mark as Answer' the post(s) that helped you
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: Recursive Classes
May 09, 2008 08:25 PM|LINK
I generally do something like:
class Foo
{
String Name{get;set;}
Int32 Id{get; set;}
IEnumerable<Foo> Children {get; set;}
}
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
gbogea
Contributor
4019 Points
576 Posts
Re: Recursive Classes
May 09, 2008 08:38 PM|LINK
What you're describing is a simplification of the Composite pattern. You'd better take a look at the link on my last post. It will be very useful to you.
-----------------
Please 'Mark as Answer' the post(s) that helped you