Jump to content
  • Advertisement
Sign in to follow this  
Palidor

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

This topic is 3559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!