On Wed, 23 Apr 2008 18:38:58 -0700, cbmeeks <cb*****@gmail.comwrote:
[...]
public class Data
{
public Data ParentClass;
public double Value;
}
...
Data d1 = new Data();
d1.Value = 100;
Data d2 = new Data();
d2.Value = 50;
d2.ParentClass = d1;
There is actually a lot more to the class but you get the idea.
Now, at this point I can easily find the value of d1 from d2 by
running "up the tree".
But my question is, how do I run down the tree?
Given the data structure you've presented, you don't. At best, you could
enumerate the linked-list (which is actually what you have here), starting
with the lowest child node and working your way up until you find the node
with the parent of interest.
For example, if a user only knew about d1, how could he know that d1
has "children" of d2? For that matter, d1 could have hundreds of
children.
I thought about using a "Data Container" that would contain a List of
Data classes but I'm not sure if that's the best way.
It sounds like what you want is to create a tree-like structure.
Typically this would involve each instance keeping a reference to the
parent (as you have here) as well as a collection of child nodes (perhaps
that's what you're suggesting? I can't really tell for sure from what you
wrote).
So your class would look something like this:
public class Data
{
public Data ParentClass;
public double Value;
public List<DataChildClasses;
}
And I use the phrase "like this" loosely, since you really shouldn't have
public fields in your class, and you'll probably want to add methods
specifically for the maintenance of the child/parent relationships, so
that when you set an instance as the child of some other instance, both
the parent and child data are automatically updated.
You might want to look at the System.Windows.Forms.Control class, as it
implements this exact kind of data structure (it's not the only example in
..NET, but it's one of the most commonly used ones).
Pete