troubles with delegates

There a class and a delegate C#

public delegate void Super();
public class Event
{
    public event Super activate ;
    public void act()
    {
       if (activate != null) activate();
    }
 }

and C++/Cli

public delegate void Super();
public ref class Event
{
public:
    event Super ^activate;
    void act()
    {
        activate();
    }
};

in C# I create multicast delegate in the class like this(methods Setplus and setminus)

public class ContainerEvents
{
    private Event obj;
    public ContainerEvents()
    {
      obj = new Event();
    }
    public Super Setplus
    {
      set { obj.activate += value; }
    }
    public Super Setminus
    {
      set { obj.activate -= value; }
    }
    public void Run()
    {
      obj.act();
    }
}

but in C++/Cli I've got an error - usage requires Event::activate to be a data member

public ref class ContainerEvents
{
    Event ^obj;
public:
    ContainerEvents()
    {
       obj = gcnew Event();
    }
    property Super^ Setplus
    {
        void set(Super^ value)
        {
            obj->activate = static_cast<Super^>(Delegate::Combine(obj->activate,value));
        }
    }

    property Super^ SetMinus
    {
        void set(Super^ value)
        { 
           obj->activate = static_cast<Super^>(Delegate::Remove(obj->activate,value));
        } 
     }

     void Run()
     {
        obj->act();
     }
};

Where is the problem?

Upvotes: 2

Views: 131

Answers (1)

Justin Aquadro
Justin Aquadro

Reputation: 2310

See: http://msdn.microsoft.com/en-us/library/ms235237(v=vs.80).aspx

C++/CLI follows the same analog as C#. It would be illegal to define this in C#:

public Super Setplus
{
    set { obj.activate = Delegate.Combine(obj.activate, value); }
}

It is the same for C++/CLI. Use the +=/-= notation that is defined in the modern syntax.

property Super^ Setplus
{
    void set(Super^ value)
    {
        obj->activate += value;
    }
}

Upvotes: 2

Related Questions