Click here to Skip to main content
15,891,704 members
Home / Discussions / C#
   

C#

 
AnswerRe: File Size Monitoring Pin
DaveyM6916-Jul-09 6:31
professionalDaveyM6916-Jul-09 6:31 
GeneralRe: File Size Monitoring Pin
See_Sharp16-Jul-09 7:31
See_Sharp16-Jul-09 7:31 
QuestionGenerics and Properties.Settings.Default ... Pin
Jammer16-Jul-09 5:25
Jammer16-Jul-09 5:25 
Question[Message Deleted] Pin
Member 311244716-Jul-09 5:15
Member 311244716-Jul-09 5:15 
AnswerRe: How to mark points on a windows from by mouse click.(c#.net) Pin
DaveyM6916-Jul-09 5:23
professionalDaveyM6916-Jul-09 5:23 
GeneralRe: How to mark points on a windows from by mouse click.(c#.net) Pin
Member 311244716-Jul-09 6:11
Member 311244716-Jul-09 6:11 
GeneralRe: How to mark points on a windows from by mouse click.(c#.net) Pin
dan!sh 16-Jul-09 6:19
professional dan!sh 16-Jul-09 6:19 
GeneralRe: How to mark points on a windows from by mouse click.(c#.net) Pin
DaveyM6916-Jul-09 6:24
professionalDaveyM6916-Jul-09 6:24 
QuestionGeting user defined attributes from external DLL with dependencies. Pin
Member 360506816-Jul-09 5:15
Member 360506816-Jul-09 5:15 
QuestionNationalInstrument Pin
peishen8816-Jul-09 5:01
peishen8816-Jul-09 5:01 
AnswerRe: NationalInstrument Pin
Rajesh R Subramanian16-Jul-09 5:08
professionalRajesh R Subramanian16-Jul-09 5:08 
GeneralRe: NationalInstrument Pin
peishen8816-Jul-09 6:05
peishen8816-Jul-09 6:05 
QuestionUsing 64 bit DLL Pin
musefan16-Jul-09 4:39
musefan16-Jul-09 4:39 
AnswerRe: Using 64 bit DLL Pin
Nagy Vilmos16-Jul-09 5:18
professionalNagy Vilmos16-Jul-09 5:18 
GeneralRe: Using 64 bit DLL Pin
musefan16-Jul-09 7:16
musefan16-Jul-09 7:16 
AnswerRe: Using 64 bit DLL [modified] Pin
dan!sh 16-Jul-09 5:30
professional dan!sh 16-Jul-09 5:30 
GeneralRe: Using 64 bit DLL Pin
Nagy Vilmos16-Jul-09 5:51
professionalNagy Vilmos16-Jul-09 5:51 
GeneralRe: Using 64 bit DLL Pin
dan!sh 16-Jul-09 5:54
professional dan!sh 16-Jul-09 5:54 
GeneralRe: Using 64 bit DLL Pin
Nagy Vilmos16-Jul-09 5:58
professionalNagy Vilmos16-Jul-09 5:58 
Questiontype.GetProperties(BindingFlags.DeclaredOnly) does not work Pin
gehbitte16-Jul-09 4:17
gehbitte16-Jul-09 4:17 
AnswerRe: type.GetProperties(BindingFlags.DeclaredOnly) does not work PinPopular
Gideon Engelberth16-Jul-09 4:28
Gideon Engelberth16-Jul-09 4:28 
GeneralRe: type.GetProperties(BindingFlags.DeclaredOnly) does not work Pin
gehbitte16-Jul-09 4:42
gehbitte16-Jul-09 4:42 
QuestionGenerics and type constraints Pin
dojohansen16-Jul-09 4:04
dojohansen16-Jul-09 4:04 
Hi,

I'm toying around with generics and have run into a little problem. My class is a tree, but this is not about whether or not one should make a tree this way - what I'm trying to do adds complexity for just a little lazy-coding benefit, so please ignore the particular example - I just want to understand if what I wanted to do can be done with generics.

Here's the TreeNode class:

abstract public class TreeNode<T>
{
    TreeNode<T> parent;
    List<TreeNode<T>> children = new List<TreeNode<T>>();
    T data;


    public TreeNode() { }


    public T Data
    {
        get { return data; }
        set { data = value; }
    }


    public TreeNode<T> Parent
    {
        get { return parent; }
        set { parent = value; }
    }


    public List<TreeNode<T>> Children
    {
        get { return children; }
        set { children = value; }
    }
}


Now, let's say we want to derive a class for organizing tags into trees. The data is just a string, and for convenience we may want a constructor that sets the text of the node. I'm trying to see if there is a way to write a utility method that would take any treenode (necessarily derived since the base class is abstract) and a collection of node *data* objects and create child nodes with the data and parent set. The following snippet illustrates the Tag and how code might use the utility method to create a hard-coded hierarchy of tags:

public class Tag : TreeNode<string>
{
  public Tag() {}
  public Tag(string text) { Data = text; }
}

class usingTags
{
  Tag getBuiltIn()
  {
      Tag root = new Tag("Built-in");
      Tag[] top = TreeUtil.Add<Tag>(root, "License", "Product");
      TreeUtil.Add<Tag>(top[0], "CPOL", "MIT", "Proprietary");
      TreeUtil.Add<Tag>(top[1], "Banco", "Acco", "Piccolo");
      return root;
  }
}


Based on this, the utility method should be something like this:
static public TNode[] Add<TNode, T>(TNode parent, params T[] data) where TNode : TreeNode<T>, new()
{
    List<TNode> list;

    if (parent == null)
        list = parent.Children;
    else
        list = new List<TNode>();

    int startIndex = list.Count;

    foreach (T item in data) list.Add(new TNode() { Data = item });
    return list.GetRange(startIndex, list.Count - startIndex).ToArray();
}


Two problems present themselves:

1) The first assignment to "list" does not build, the compiler complaining it cannot convert List<TreeNode<T>> to List<TNode>, which I think is odd since I have defined a type constraint which guarantees that TNode either is or is derived from TreeNode<T>.

2) If I change that assignment so it assigns null instead, just to be able to build, the code *using* it does not compile. I thought the type of T could be inferred, since TNode is TreeNode<T> and the params T[] parameter is an array of strings. But the compiler just says the method requries two type parameters.

Is there a way to solve these problems so the user of the utility method need only supply the node type to create and the actual values for node data?
AnswerRe: Generics and type constraints Pin
OriginalGriff16-Jul-09 4:13
mveOriginalGriff16-Jul-09 4:13 
GeneralRe: Generics and type constraints Pin
dojohansen16-Jul-09 5:05
dojohansen16-Jul-09 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.