views:

131

answers:

3

I want to create a completely generic treeview like structure. some thing like this:

public class TreeView<T, K, L>
{
    public T source;
    public K parent;
    public List<L> children;
}

as you can see in this class source, parent and also the children, all have a different generic data type. also i want my tree view to have unlimited number of levels (not just 3). this way when i want to work with my nodes in the code, all of them are going to be strongly typed. not just objects that i need to convert them to their original type.

is it possible to create this kind of structure in c#, a treeview which all of its nodes are strongly typed?

thanks

A: 

Well, apart from being a tree, you will have some underlying data. For example a directory tree. The attributes of directory are its name and a list of child directories. We start off by defining a generic TreeItem.

public class TreeItem<T> {
  public TreeItem() {
    Children = new List<TreeItem<T>>();
  }

  public void AddChild(T data) {
    Children.Add(new TreeItem<T>{Data = data, Parent = this});
  }

  public List<TreeItem<T>> Children{get;set;}
  public TreeItem<T> Parent {get;set;}
  public T Data {get;set;}
}

So a simple directory tree is just a TreeItem<string>:

var directories = new TreeItem<string> { Data="root" };
directories.AddChild("child1");
directories.AddChild("child2");
directories.AddChild("child3");

This would create a tree like this:

root
|- child1
|- child2
|- child3

The only way to make a completely generic tree view is to have the same types for current node, node above and all the child nodes, otherwise you have to fix the structure at compile time and only support a set hierarchy.

Igor Zevaka
Yes I know how to create this kind of treeview, when all the nodes have the same data type. but my question is about how to make it with different data types and also how to make it strongly-typed
Nima Rikhtegar
A: 

After reading Igors answer and your comment and can just say this isn't possible in such a way. All you can do is using as T some base type that all classes have in common, like a base class or an Interface.

But if you need somewhere in your code the specific type you'll need to cast it which may lead to some kind of if-return or if-else-if structure like:

SpecificType specType = commonType as SpecificType;
if(specType != null)
{
    //Do something...
    return;
}

AnotherSpecifcType specType2 = commonType as AnotherSpecifcType;
if(specType2 != null)
{
    //Do something...
    return;
}

But that is all you can do.

Oliver
A: 

This is the big problem with tree data structures. It's easy to define homogeneous trees, but in the real world, trees are often composed of heterogeneous object types. A good example of this is a file system, where the tree contains drives, folders, and files.

You can only create a type safe tree if you know exactly the shape of the tree at compile time. Of course, this eliminates every real world use case for trees.

bbudge