Sign in to follow this  
Palidor

[.net] Question: Add/Remove Different Objects to Tree and Corresponding List

Recommended Posts

Hi there, i posted a couple of days ago and got a very helpful response, but after careful consideration, i realized there must be a better way to automate my data. I currently have a Treeview control that houses a list of CAnimationSets. As of right now, i have a bunch of if/else statements to handle adding and removing of different types of objects from the tree, because he tree also handles these objects being added and removed from the List that is attached to the tree. I think that maybe i am currently approaching this the wrong way. What i have is a List of CAnimationSets, which each contain lists of other day inside them. I want to store each of this data in a treeview so the user can expand and look at only what they need to, and also add and remove certain items from the various lists within the master list. Does anyone have any suggestions as to what i should do to make my code more extensible for future additions to the project? A bunch of if/else statements seems like really bad coding practice... Thank you in advance! P.S. I'm doing this project for fun and experience, so any and all thoughts will help me :-)

Share this post


Link to post
Share on other sites
Not sure I understand exactly what you are doing, but sounds like you could use an interface.


interface ITreeViewThingy
{
void AddToTree(TreeView tree);
}

...

public class CAnimationSets : ITreeViewThingy
{
List<object> childObjects;
...
public void AddToTree(TreeView tree)
{
tree.Controls.Add(this);
foreach (object child in childObjects)
{
ITreeViewThingy childTVT = child as ITreeViewThingy;
if (childTVT != null)
childTVT.AddToTree(tree);
}
}
}




Basically, you have an interface which says that, "any class that implements this interface can be added to the TreeView", then you implement how it is added to the tree. The loop in there that I added just iterates through all child objects, tries to convert them to the interface (childTVT will be null if the conversion failed, but besides that, it is like using (ITreeViewThingy)child), and if it was converted, adds it to the tree, too. It'd end up recursively doing this until every single object that implements ITreeViewThingy is added.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this