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

C#

 
QuestionGenerics and type constraints Pin
dojohansen16-Jul-09 4:04
dojohansen16-Jul-09 4:04 
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 
Thanks for the reply.

I see now that the normal rules of assignment are violated; List<TDerived> is not a List<TBase> even if TDerived derives from TBase. They are just two different constructed types, and since it is so there is no way to convert between them.

The problem with using static public TreeNode<T>[] Add<T>(TreeNode<T> parent, params T[] data) is that I cannot put a type constraint on the constructed type; in order to add new nodes the constructed type must have a default constructor.

This problem can be solved by keeping the method declaration but use a List<TBase> as local variable instead:

static public TNode[] Add<TNode, T>(TNode parent, params T[] data) where TNode : TreeNode<T>, new()
{
    List<TreeNode<T>> list;

    if (parent != null)
        list = parent.Children;
    else
        list = new List<TreeNode<T>>();

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


In the end it's all futile though. Basically, I wanted to write a generic TreeNode type which can be parameterized by type of node data. But rather than the use constructed types directly, such as TreeNode<string>, I wanted to derive non-generic types from constructed types, potentially adding functionality to each type of tree node, as in class Tag : TreeNode<string> { ... }, but I didn't stop to think; Tag.Children would still be List<TreeNode<string>> rather than List<Tag>, the desired type.
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 
GeneralRe: If statement with ? and : characters Pin
soso_online16-Jul-09 2:09
soso_online16-Jul-09 2:09 
AnswerRe: If statement with ? and : characters Pin
Luc Pattyn16-Jul-09 1:25
sitebuilderLuc Pattyn16-Jul-09 1:25 
GeneralRe: If statement with ? and : characters Pin
soso_online16-Jul-09 2:09
soso_online16-Jul-09 2:09 
AnswerRe: If statement with ? and : characters Pin
Pete O'Hanlon16-Jul-09 1:54
mvePete O'Hanlon16-Jul-09 1:54 
GeneralRe: If statement with ? and : characters Pin
soso_online16-Jul-09 2:08
soso_online16-Jul-09 2:08 
QuestionWindows Form Datagridview Pin
Rajeshwar Code- Developer16-Jul-09 0:58
Rajeshwar Code- Developer16-Jul-09 0:58 
AnswerRe: Windows Form Datagridview Pin
Vimalsoft(Pty) Ltd16-Jul-09 1:14
professionalVimalsoft(Pty) Ltd16-Jul-09 1:14 

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.