3

Below is my code in c#...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        TestPointer test = new TestPointer();
        test.function1(function2);   // Error here: The name 'function2' does not exist in     current context
    }
}

class TestPointer
{
    private delegate void fPointer(); // point to every functions that it has void as return value and with no input parameter 
    public void function1(fPointer ftr)
    {
        fPointer point = new fPointer(ftr);
        point();
    }

    public void function2()
    {
        Console.WriteLine("Bla");
    }
}

How can I call a callback function by passing function refernce in main function?... I'm new to c#

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28

4 Answers4

3

test.function1(test.function2) should do it.

You'll also need

public delegate void fPointer();

instead of

private delegate void fPointer();
Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Ah, you have to mark your `delegate` as `public` too in order for it to be used outside the class. It now compiles and runs fine for me. – Rawling Sep 18 '12 at 11:08
1

You could do it with an Action:

class Program
    {
        static void Main(string[] args)
        {
            TestPointer test = new TestPointer();
            test.function1(() => test.function2());   // Error here: The name 'function2' does not exist in     current context

            Console.ReadLine();
        }
    }

    class TestPointer
    {
        private delegate void fPointer(); // point to every functions that it has void as return value and with no input parameter 
        public void function1(Action ftr)
        {
            ftr();
        }

        public void function2()
        {
            Console.WriteLine("Bla");
        }
    }
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
1

There are 2 problems with your code:

    TestPointer test = new TestPointer();
    test.function1(function2);   

Here, there is no variable called function2 in scope. What you want to do is call it like this:

    test.function1(test.function2);   

The test.function2 is in fact a method group, which in this case will be converted by the compiler to a delegate. On to the next problem:

private delegate void fPointer(); 
public void function1(fPointer ftr)

You declared the delegate as private. It should be public. A delegate is a special kind of type, but it is still a type (you can declare variables of 'em, which is exactly what you do when you declare the argument to function1). When declared as private, the type is not visible from outside the class TestPointer and thus can not be used as an argument to a public method.

Finally, not really an error but the way you call the delegate can be simplified:

    ftr();

So here's your corrected code:

using System;

class Program
{
    static void Main(string[] args)
    {
        TestPointer test = new TestPointer();
        test.function1(test.function2);   
    }
}

class TestPointer
{
    public delegate void fPointer(); 
    public void function1(fPointer ftr)
    {
        ftr();
    }

    public void function2()
    {
        Console.WriteLine("Bla");
    }
}
Community
  • 1
  • 1
jeroenh
  • 26,362
  • 10
  • 73
  • 104
0

You need to make function2 static or pass text.function2.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • If you make `function2` `static` you'll need to pass `TestPointer.function2`. If not, you'll need to pass `test.function2`, not `text.funxtion2`. – Rawling Sep 18 '12 at 11:06