how to use anonymous generic delegate in C# 2.0

Posted by matti on Stack Overflow See other posts from Stack Overflow or by matti
Published on 2010-05-04T07:17:43Z Indexed on 2010/05/04 7:28 UTC
Read the original article Hit count: 464

Filed under:
|
|

Hi. I have a class called NTree:

class NTree<T>
{
    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;
            Traverse(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)
            Traverse(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

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics