Search code examples
c#delegatesmanaged-c++

How do I add a delegate to event in managed c++ 2010?


I've got following code in c#

public class Shedluer
{
    public delegate void TaskCompletedDelegate(ulong taskId);
    public static event TaskCompletedDelegate OnTaskCompleted;
}

Here's how I'm trying to use it in c++.net:

shedluer->OnTaskCompleted+=gcnew Shedluer::TaskCompletedDelegate(OnTaskFinished);

where OnTaskFinished is a non-static method declared inside a ref class.

I've seen a bunch of examples for c++.net 2007, but those won't compile in vs 2010.

How do I add a delegate to event in managed c++ 2010?


Solution

  • Supposing the OnTaskFinished is in the current class, and that ref class is of type MyClass. In that case, you'd write:

    shedluer->OnTaskCompleted += gcnew Shedluer::TaskCompletedDelegate(this, &MyClass::OnTaskFinished);