2

I have a class called NTree:

class NTree<T>
{

    delegate bool TreeVisitor<T>(T nodeData);

    public NTree(T data)
    {
        this.data = data;
        children = new List<NTree<T>>();
        _stopTraverse = false;
    }

        ...

    public void Traverse(NTree<T> node, TreeVisitor<T> visitor)
    {
        try
        {
            _stopTraverse = false;
            TraverseInternal(node, visitor);
        }
        finally
        {
            _stopTraverse = false;
        }
    }

    private void TraverseInternal(NTree<T> node, TreeVisitor<T> visitor)
    {
        if (_stopTraverse)
            return;

        if (!visitor(node.data))
        {
            _stopTraverse = true;
        }
        foreach (NTree<T> kid in node.children)
            TraverseInternal(kid, visitor);
    }

When I try to use Traverse with anonymous delegate I get:

Argument '2': cannot convert from 'anonymous method' to 'NisConverter.TreeVisitor'

The code:

tTable srcTable = new tTable();
DataRow[] rows;
rootTree.Traverse(rootTree, delegate(TableRows tr)
    {
        if (tr.TableName == srcTable.mappingname)
        {
            rows = tr.Rows;
            return false;
        }
    });

This however produces no errors:

    static bool TableFinder<TableRows>(TableRows tr)
    {
        return true;
    }

...

rootTree.Traverse(rootTree, TableFinder);

I have tried to put "arrowhead-parenthisis" and everything to anonymous delegate but it just does not work. Please help me!

Thanks & BR -Matti

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
char m
  • 7,840
  • 14
  • 68
  • 117
  • What is the declaration of TreeVisitor ? Be careful that you have infinite recursion in Traverse (shouldn't it call TravereInternal ?) and you are setting _stopTraverse to false twice in Traverse, so TraverseInternal won't do anything. – Timores May 04 '10 at 07:32
  • @timores: thanks, both fixed already :) I can fix the functionality l8r. I need to know how to do it with anonymous delegate. – char m May 04 '10 at 07:35
  • We still need to see the declaration of `TreeVisitor` – AakashM May 04 '10 at 07:40
  • @Aakas: I updated it 10mins ago. Is there some problem? I see it upmost declaration in the class :) – char m May 04 '10 at 07:43

2 Answers2

4

The anonymous delegate you posted lacks the return of a boolean value (most probably the value true when the if(...) guard is false. Thus, it signature is actually void (TableRow) instead of bool (TableRow), and the compiler is unable to make the conversion.

So the syntax should be:

tTable srcTable = new tTable();  DataRow[] rows;  rootTree.Traverse(rootTree, delegate(TableRows tr) 
    { 
        if (tr.TableName == srcTable.mappingname) 
        { 
            rows = tr.Rows; 
            return false; 
        } 
        return true;
    });
Erik Burigo
  • 317
  • 1
  • 12
1

The declaration of TreeVisitor is wrong: it introduces a new type parameter (which conflicts with the declaration of NTree). Just remove the template stuff and you get:

delegate bool TreeVisitor(T nodeData);

Then you can have:

class X
{

    void T()
    {
        NTree<int> nti = new NTree<int>(2);

        nti.Traverse(nti, delegate(int i) { return i > 4; });

    }
}
Timores
  • 14,439
  • 3
  • 46
  • 46