Click here to Skip to main content
15,742,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is one of the code snippet I am trying to do for delegate callback

  public delegate void FooCallbackType(int a, int b, int c);

    class CMyClass
        public void FunctionToCall(int a, int b, int c)
            MessageBox.Show("I am first class");
            // This is the callback

        public void Foo()
            FooCallbackType myDelegate = new FooCallbackType(this.FunctionToCall);
            // Now you can pass that to the function
            // that needs to call you back.


    class CMyClassWithStaticCallback

        public static void StaticFunctionToCall(int a, int b, int c)
            MessageBox.Show("I am second class"); // This is the callback

        public static void Foo()

            FooCallbackType myDelegate = new FooCallbackType(CMyClass.StaticFunctionToCall);


        static void Main()



1.In this example this line of code is showing error

FooCallbackType myDelegate = new FooCallbackType(CMyClass.StaticFunctionToCall);

because I think this function is belong to the Class "CMyClassWithStaticCallback not the "CMyClass"

so I don't understand why/how it is "CMyClass.StaticFunctionToCall"

2.What should I write in the Main section.

because I want to see the output to understand and comapre the result.

any help will be appreciated.
Updated 3-Feb-11 18:23pm

1 solution

This is what it is: you did not provide method called CMyClassWithStaticCallback.

Forget it, it does not matter! Your syntax is so obsolete! Consider this:

//anonymous method, good for C# v.2 and above:
FooCallbackType myDelegate = delegate(int a, int b, int c) {
    //body of your delegate code here...

//same thing with lambda syntax, good with C# v > 2:
myDelegate = new FooCallbackType((int a, int b, int c) => {
    //body of your delegate code here...

Also, your Main may not compile you have another Main. This is because this is used as an entry point, most likely. To change the entry point, see you project's Property, Application tab, "Startup object".

Share this answer
Christy A C 4-Feb-11 0:58am    
Sergey Alexandrovich Kryukov 4-Feb-11 1:01am    
You're welcome. I hope it helps you. Come again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900