Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Generics and type constraints Pin
Dan Neely16-Jul-09 5:45
Dan Neely16-Jul-09 5:45 
GeneralRe: Generics and type constraints Pin
dojohansen16-Jul-09 6:35
dojohansen16-Jul-09 6:35 
GeneralRe: Generics and type constraints Pin
Dan Neely16-Jul-09 7:09
Dan Neely16-Jul-09 7:09 
AnswerRe: Generics and type constraints Pin
Gideon Engelberth16-Jul-09 7:16
Gideon Engelberth16-Jul-09 7:16 
GeneralRe: Generics and type constraints Pin
dojohansen16-Jul-09 22:00
dojohansen16-Jul-09 22:00 
QuestionResetting Objects to startup-state Pin
chrisx5116-Jul-09 3:02
chrisx5116-Jul-09 3:02 
AnswerRe: Resetting Objects to startup-state Pin
Luc Pattyn16-Jul-09 3:24
sitebuilderLuc Pattyn16-Jul-09 3:24 
AnswerRe: Resetting Objects to startup-state Pin
dojohansen16-Jul-09 5:30
dojohansen16-Jul-09 5:30 
QuestionHow to Provide Confirmation Link to the New User in C# Pin
ChandrakanthGaddam16-Jul-09 1:35
ChandrakanthGaddam16-Jul-09 1:35 
AnswerRe: How to Provide Confirmation Link to the New User in C# Pin
Pete O'Hanlon16-Jul-09 1:57
mvePete O'Hanlon16-Jul-09 1:57 
Questionupdate progress bar concurrently in windows form Pin
Praveen Raghuvanshi16-Jul-09 1:33
professionalPraveen Raghuvanshi16-Jul-09 1:33 
AnswerRe: update progress bar concurrently in windows form Pin
monstale16-Jul-09 2:11
monstale16-Jul-09 2:11 
AnswerRe: update progress bar concurrently in windows form Pin
MumbleB16-Jul-09 2:19
MumbleB16-Jul-09 2:19 
QuestionIf statement with ? and : characters Pin
soso_online16-Jul-09 1:00
soso_online16-Jul-09 1:00 
AnswerRe: If statement with ? and : characters Pin
Tamer Oz16-Jul-09 1:13
Tamer Oz16-Jul-09 1:13 

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.