views:

107

answers:

5

The following example works, but how can I change it so that instead of executing the anonymous method, it executes my existing callback method OnCreateOfferComplete()?

using System;

namespace TestCallBack89393
{
    class Program
    {
        static void Main(string[] args)
        {
            OfferManager offerManager = new OfferManager();
            offerManager.CreateOffer("test", () => Console.WriteLine("finished."));

            //offerManager.CreateOffer("test", OnCreateOfferComplete ); 
            //above line gives error: an object reference is required 
            //for a non-static field...



            Console.ReadLine();

        }

        private void OnCreateOfferComplete()
        {
            Console.WriteLine("finished");
        }
    }


    public class OfferManager
    {

        public void CreateOffer(string idCode, Action onComplete)
        {
            if (onComplete != null)
                onComplete();
        }
    }
}
+2  A: 

Make method OnCreateOfferComplete static. This should sove your problem.

elder_george
+1  A: 

Make OnCreateOfferComplete method static.

Vitaliy Liptchinsky
+1  A: 

The problem is that your method OnCreateOfferComplete() needs to be static.

Brian ONeil
+1  A: 

The problem is that you are calling CreateOffer from a static method (OnCreateOfferComplete is an instance method).

In this case, just declare your OnCreateOfferComplete method static.

Daren Thomas
+1  A: 

I think it should be static:

private static void OnCreateOfferComplete()
{
    Console.WriteLine("finished");
}

... because you are calling it from the static Main method.

Paulo Guedes